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

N4 Segfault with unsigned char/float #109

Open
muschellij2 opened this issue Dec 5, 2019 · 46 comments
Open

N4 Segfault with unsigned char/float #109

muschellij2 opened this issue Dec 5, 2019 · 46 comments

Comments

@muschellij2
Copy link
Collaborator

Describe the bug
Segfault when mask is 2D unsigned char.

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
fname = getANTsRData("r16")
in_img = antsImageRead(fname)
full_image = in_img > -1
n4 = n4BiasFieldCorrection(in_img, mask = full_image)

 *** caught segfault ***
address 0x10169eba0, cause 'memory not mapped'

Traceback:
 1: 
 *** caught segfault ***
address 0x10169b000, cause 'memory not mapped'
.helpn4BiasFieldCorrection(args)
 2: n4BiasFieldCorrection(in_img, mask = full_image2)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
full_image2 = antsImageClone(full_image, out_pixeltype = "float")
n4 = n4BiasFieldCorrection(in_img, mask = full_image2)

 *** caught segfault ***
address 0x1106dd000, cause 'memory not mapped'

Traceback:
 1: .helpn4BiasFieldCorrection(args)
 2: n4BiasFieldCorrection(in_img, mask = full_image)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
library(ANTsRCore)
fname = getANTsRData("r16")
in_img = antsImageRead(fname)

mask = getMask(in_img)
n4 = n4BiasFieldCorrection(in_img, mask = mask)

 *** caught segfault ***

 *** caught segfault ***
address 0x104b81000, cause 'memory not mapped'

Traceback:
 1: .helpn4BiasFieldCorrection(args)
 2: n4BiasFieldCorrection(in_img, mask = mask)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

Created on 2019-12-05 by the reprex package (v0.3.0.9000)

Session info
sessioninfo::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2019-12-05                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version   date       lib source                            
#>  ANTsRCore   * 0.7.3     2019-11-26 [1] local                             
#>  assertthat    0.2.1     2019-03-21 [1] CRAN (R 3.6.0)                    
#>  cli           1.1.0     2019-03-19 [1] CRAN (R 3.6.0)                    
#>  crayon        1.3.4     2017-09-16 [1] CRAN (R 3.6.0)                    
#>  digest        0.6.23    2019-11-23 [1] CRAN (R 3.6.0)                    
#>  evaluate      0.14      2019-05-28 [1] CRAN (R 3.6.0)                    
#>  highr         0.8       2019-03-20 [1] CRAN (R 3.6.0)                    
#>  htmltools     0.4.0     2019-10-04 [1] CRAN (R 3.6.0)                    
#>  ITKR          0.5.2     2019-11-05 [1] Github (stnava/ITKR@fe97efa)      
#>  knitr         1.24.3    2019-08-28 [1] Github (muschellij2/knitr@abcea3d)
#>  lattice       0.20-38   2018-11-04 [1] CRAN (R 3.6.0)                    
#>  magrittr      1.5       2014-11-22 [1] CRAN (R 3.6.0)                    
#>  Matrix        1.2-17    2019-03-22 [1] CRAN (R 3.6.0)                    
#>  Rcpp          1.0.3     2019-11-08 [1] CRAN (R 3.6.0)                    
#>  RcppEigen     0.3.3.5.0 2018-11-24 [1] CRAN (R 3.6.0)                    
#>  rlang         0.4.2     2019-11-23 [1] CRAN (R 3.6.0)                    
#>  rmarkdown     1.16      2019-10-01 [1] CRAN (R 3.6.0)                    
#>  sessioninfo   1.1.1     2018-11-05 [1] CRAN (R 3.6.0)                    
#>  stringi       1.4.3     2019-03-12 [1] CRAN (R 3.6.0)                    
#>  stringr       1.4.0     2019-02-10 [1] CRAN (R 3.6.0)                    
#>  withr         2.1.2     2018-03-15 [1] CRAN (R 3.6.0)                    
#>  xfun          0.11      2019-11-12 [1] CRAN (R 3.6.0)                    
#>  yaml          2.2.0     2018-07-25 [1] CRAN (R 3.6.0)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
@muschellij2
Copy link
Collaborator Author

This works however:

library(ANTsRCore)
 dims = c(50, 50)
 img<-makeImage(imagesize = dims, rnorm(prod(dims)) )
 n4img<-n4BiasFieldCorrection(img)
 n4img<-n4BiasFieldCorrection(img, mask = img > 0)

@muschellij2
Copy link
Collaborator Author

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
fname = getANTsRData("r16")
in_img = antsImageRead(fname)
n4 = n4BiasFieldCorrection(in_img)
plot(n4)

#> NULL

Created on 2019-12-05 by the reprex package (v0.3.0.9000)

Session info
sessioninfo::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2019-12-05                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version   date       lib source                            
#>  ANTsRCore   * 0.7.3     2019-11-26 [1] local                             
#>  assertthat    0.2.1     2019-03-21 [1] CRAN (R 3.6.0)                    
#>  cli           1.1.0     2019-03-19 [1] CRAN (R 3.6.0)                    
#>  crayon        1.3.4     2017-09-16 [1] CRAN (R 3.6.0)                    
#>  curl          4.2       2019-09-24 [1] CRAN (R 3.6.0)                    
#>  digest        0.6.23    2019-11-23 [1] CRAN (R 3.6.0)                    
#>  evaluate      0.14      2019-05-28 [1] CRAN (R 3.6.0)                    
#>  highr         0.8       2019-03-20 [1] CRAN (R 3.6.0)                    
#>  htmltools     0.4.0     2019-10-04 [1] CRAN (R 3.6.0)                    
#>  httr          1.4.1     2019-08-05 [1] CRAN (R 3.6.0)                    
#>  ITKR          0.5.2     2019-11-05 [1] Github (stnava/ITKR@fe97efa)      
#>  knitr         1.24.3    2019-08-28 [1] Github (muschellij2/knitr@abcea3d)
#>  lattice       0.20-38   2018-11-04 [1] CRAN (R 3.6.0)                    
#>  magrittr      1.5       2014-11-22 [1] CRAN (R 3.6.0)                    
#>  Matrix        1.2-17    2019-03-22 [1] CRAN (R 3.6.0)                    
#>  mime          0.7       2019-06-11 [1] CRAN (R 3.6.0)                    
#>  misc3d      * 0.8-4     2013-01-25 [1] CRAN (R 3.6.0)                    
#>  pixmap      * 0.4-11    2011-07-19 [1] CRAN (R 3.6.0)                    
#>  R6            2.4.1     2019-11-12 [1] CRAN (R 3.6.0)                    
#>  Rcpp          1.0.3     2019-11-08 [1] CRAN (R 3.6.0)                    
#>  RcppEigen     0.3.3.5.0 2018-11-24 [1] CRAN (R 3.6.0)                    
#>  rlang         0.4.2     2019-11-23 [1] CRAN (R 3.6.0)                    
#>  rmarkdown     1.16      2019-10-01 [1] CRAN (R 3.6.0)                    
#>  sessioninfo   1.1.1     2018-11-05 [1] CRAN (R 3.6.0)                    
#>  stringi       1.4.3     2019-03-12 [1] CRAN (R 3.6.0)                    
#>  stringr       1.4.0     2019-02-10 [1] CRAN (R 3.6.0)                    
#>  withr         2.1.2     2018-03-15 [1] CRAN (R 3.6.0)                    
#>  xfun          0.11      2019-11-12 [1] CRAN (R 3.6.0)                    
#>  xml2          1.2.2     2019-08-09 [1] CRAN (R 3.6.0)                    
#>  yaml          2.2.0     2018-07-25 [1] CRAN (R 3.6.0)                    
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

@muschellij2
Copy link
Collaborator Author

Also weirdly it may be due to dimensions!?

No segfault:

library(ANTsRCore)

for (i in 1:5) {
  print(i)
  dims = c(50, 50)
  img <-makeImage(imagesize = dims, rnorm(prod(dims)) )
  n4img<-n4BiasFieldCorrection(img)
  n4img<-n4BiasFieldCorrection(img, mask = img > 0)
}

Will segfault

for (i in 1:5) {
  print(i)
  dims = c(256, 256)
  img <-makeImage(imagesize = dims, rnorm(prod(dims)) )
  n4img<-n4BiasFieldCorrection(img)
  # segfault
  sum(img > 0)
  n4img<-n4BiasFieldCorrection(img, mask = img > 0)
  n4img
}

@muschellij2
Copy link
Collaborator Author

Reinstalling to see if this goes away.

@stnava
Copy link
Member

stnava commented Dec 5, 2019 via email

@muschellij2
Copy link
Collaborator Author

Still getting same error after installing locally. GitHub install may be different - but something is off.

@stnava
Copy link
Member

stnava commented Dec 5, 2019 via email

@muschellij2
Copy link
Collaborator Author

Still failingwith

─ Packages ───────────────────────────────────────────────────────────────────
 package   * version   date       lib source                          
 ANTsRCore * 0.7.3     2019-12-11 [1] Github (ANTsX/ANTsRCore@416d8f1)
 ITKR        0.5.2     2019-11-05 [1] Github (stnava/ITKR@fe97efa)    

Can you send your session info? Having problems in 3D with mask in n4 and so is @adigherman

@stnava
Copy link
Member

stnava commented Jan 16, 2020

I suspect this was fixed by @ntustison just yesterday ( in ANTs ) ...

@muschellij2
Copy link
Collaborator Author

That's great news! Either way, here should be a reproducible example:
Archive.zip

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
library(neurobase)
#> Loading required package: oro.nifti
#> oro.nifti 0.10.1
#> 
#> Attaching package: 'oro.nifti'
#> The following objects are masked from 'package:ANTsRCore':
#> 
#>     origin, origin<-
library(extrantsr)
#> 
#> Attaching package: 'extrantsr'
#> The following object is masked from 'package:neurobase':
#> 
#>     zero_pad
#> The following objects are masked from 'package:oro.nifti':
#> 
#>     origin, origin<-
#> The following objects are masked from 'package:ANTsRCore':
#> 
#>     origin, origin<-
destfile  = tempfile(fileext = ".zip")
download.file("https://github.com/ANTsX/ANTsRCore/files/4073596/Archive.zip",
              destfile = destfile)
unzip(destfile, exdir = tempdir())
files = "IXI002-Guys-0828-T1_bias_correct.nii.gz"
files = file.path(tempdir(), files)
img = antsImageRead(files)


maskfile = "IXI002-Guys-0828-T1_brain_mask.nii.gz"
maskfile = file.path(tempdir(), maskfile)
mask = antsImageRead(maskfile)
img
#> antsImage
#>   Pixel Type          : float 
#>   Components Per Pixel: 1 
#>   Dimensions          : 150x256x202 
#>   Voxel Spacing       : 1.19999694824219x0.9375x0.9375 
#>   Origin              : -90.15966 120.7078 -82.64942 
#>   Direction           : 1 3.756096e-08 2.323527e-09 3.756096e-08 -0.9923758 -0.1232487 2.323527e-09 -0.1232487 0.9923758 
#>   Filename           : /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpJ141o8/IXI002-Guys-0828-T1_bias_correct.nii.gz
mask
#> antsImage
#>   Pixel Type          : float 
#>   Components Per Pixel: 1 
#>   Dimensions          : 150x256x202 
#>   Voxel Spacing       : 1.19999694824219x0.9375x0.9375 
#>   Origin              : -90.15966 120.7078 -82.64942 
#>   Direction           : 1 3.756095e-08 2.323527e-09 3.756095e-08 -0.9923758 -0.1232487 2.323527e-09 -0.1232487 0.9923758 
#>   Filename           : /var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpJ141o8/IXI002-Guys-0828-T1_brain_mask.nii.gz
range(img)
#> [1]   0.0000 988.2431
range(mask)
#> [1] 0 1
unique(c(as.array(mask)))
#> [1] 0 1

ortho2(img)

ortho2(mask)

outfiles = tempfile(fileext = ".nii.gz")
bc = n4BiasFieldCorrection(img, verbose = TRUE)
antsImageWrite(bc, outfiles)
range(bc)
#> [1]    0.00 2161.69
ortho2(bc, mask)

masked_image = maskImage(bc, mask)
range(masked_image)
#> [1]    0.000 1163.379
ortho2(masked_image)

ofile1 = outfiles
f = outfiles[1]
fslr::fslmask(file = f, mask = maskfile,
        outfile = f,
        retimg = FALSE)
#> Warning in get.fsl(): Setting fsl.path to /usr/local/fsl
#> Warning in get.fsloutput(): Can't find FSLOUTPUTTYPE, setting to NIFTI_GZ
#> FSLDIR='/usr/local/fsl'; PATH=${FSLDIR}/bin:${PATH};export PATH FSLDIR; sh "${FSLDIR}/etc/fslconf/fsl.sh"; FSLOUTPUTTYPE=NIFTI_GZ; export FSLOUTPUTTYPE; ${FSLDIR}/bin/fslmaths "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpJ141o8/file597b5872170e.nii.gz" -mas "/private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/RtmpJ141o8/IXI002-Guys-0828-T1_brain_mask.nii.gz"  "/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T//RtmpJ141o8/file597b5872170e"
#> [1] 0
fsl_masked_image = antsImageRead(f)
ortho2(fsl_masked_image)
range(fsl_masked_image)
#> [1]    0.000 1163.379
all.equal(as.array(fsl_masked_image), as.array(masked_image))
#> [1] TRUE

Here is the segfault

bc2 = n4BiasFieldCorrection(masked_image, mask = mask, verbose = TRUE)
Running N4 for 3-dimensional images.

Original intensity range:  [0, 1163.38]

***********************************************************
Warning:  Your input image contains nonpositive values
which could cause failure or problematic results.  A
possible workaround would be to:
   1. rescale your image to positive values e.g., [10,100].
   2. run N4 on your rescaled image.
   3. (optional) rescale the N4 output to the original
      intensity range.
***********************************************************


Specified spline distance: 200mm
  original image size:  [150, 256, 202]
  padded image size:  [168, 428, 214]
  number of control points:  [4, 5, 4]

Current level = 1
  Iteration 1 (of 50).   Current convergence value = 0.000170185 (threshold = 1e-07)
  Iteration 2 (of 50).   Current convergence value = 0.000169809 (threshold = 1e-07)
  Iteration 3 (of 50).   Current convergence value = 0.000169486 (threshold = 1e-07)
  Iteration 4 (of 50).   Current convergence value = 0.000169141 (threshold = 1e-07)
  Iteration 5 (of 50).   Current convergence value = 0.000168838 (threshold = 1e-07)
  Iteration 6 (of 50).   Current convergence value = 0.000168541 (threshold = 1e-07)
  Iteration 7 (of 50).   Current convergence value = 0.000168237 (threshold = 1e-07)
  Iteration 8 (of 50).   Current convergence value = 0.000167974 (threshold = 1e-07)
  Iteration 9 (of 50).   Current convergence value = 0.000167724 (threshold = 1e-07)
  Iteration 10 (of 50).   Current convergence value = 0.000167464 (threshold = 1e-07)
  Iteration 11 (of 50).   Current convergence value = 0.000167247 (threshold = 1e-07)
  Iteration 12 (of 50).   Current convergence value = 0.000166963 (threshold = 1e-07)
  Iteration 13 (of 50).   Current convergence value = 0.000166759 (threshold = 1e-07)
  Iteration 14 (of 50).   Current convergence value = 0.000166538 (threshold = 1e-07)
  Iteration 15 (of 50).   Current convergence value = 0.000166299 (threshold = 1e-07)
  Iteration 16 (of 50).   Current convergence value = 0.000166066 (threshold = 1e-07)
  Iteration 17 (of 50).   Current convergence value = 0.000165876 (threshold = 1e-07)
  Iteration 18 (of 50).   Current convergence value = 0.000165647 (threshold = 1e-07)
  Iteration 19 (of 50).   Current convergence value = 0.000165474 (threshold = 1e-07)
  Iteration 20 (of 50).   Current convergence value = 0.000165285 (threshold = 1e-07)
  Iteration 21 (of 50).   Current convergence value = 0.000165109 (threshold = 1e-07)
  Iteration 22 (of 50).   Current convergence value = 0.00016496 (threshold = 1e-07)
  Iteration 23 (of 50).   Current convergence value = 0.000164833 (threshold = 1e-07)
  Iteration 24 (of 50).   Current convergence value = 0.00016471 (threshold = 1e-07)
  Iteration 25 (of 50).   Current convergence value = 0.000164579 (threshold = 1e-07)
  Iteration 26 (of 50).   Current convergence value = 0.000164442 (threshold = 1e-07)
  Iteration 27 (of 50).   Current convergence value = 0.000164275 (threshold = 1e-07)
  Iteration 28 (of 50).   Current convergence value = 0.000164179 (threshold = 1e-07)
  Iteration 29 (of 50).   Current convergence value = 0.000164016 (threshold = 1e-07)
  Iteration 30 (of 50).   Current convergence value = 0.000163885 (threshold = 1e-07)
  Iteration 31 (of 50).   Current convergence value = 0.00016375 (threshold = 1e-07)
  Iteration 32 (of 50).   Current convergence value = 0.000163598 (threshold = 1e-07)
  Iteration 33 (of 50).   Current convergence value = 0.00016348 (threshold = 1e-07)
  Iteration 34 (of 50).   Current convergence value = 0.00016336 (threshold = 1e-07)
  Iteration 35 (of 50).   Current convergence value = 0.00016327 (threshold = 1e-07)
  Iteration 36 (of 50).   Current convergence value = 0.000163142 (threshold = 1e-07)
  Iteration 37 (of 50).   Current convergence value = 0.000163054 (threshold = 1e-07)
  Iteration 38 (of 50).   Current convergence value = 0.000162918 (threshold = 1e-07)
  Iteration 39 (of 50).   Current convergence value = 0.00016276 (threshold = 1e-07)
  Iteration 40 (of 50).   Current convergence value = 0.000162607 (threshold = 1e-07)
  Iteration 41 (of 50).   Current convergence value = 0.000162515 (threshold = 1e-07)
  Iteration 42 (of 50).   Current convergence value = 0.000162417 (threshold = 1e-07)
  Iteration 43 (of 50).   Current convergence value = 0.000162307 (threshold = 1e-07)
  Iteration 44 (of 50).   Current convergence value = 0.000162165 (threshold = 1e-07)
  Iteration 45 (of 50).   Current convergence value = 0.000162049 (threshold = 1e-07)
  Iteration 46 (of 50).   Current convergence value = 0.000161911 (threshold = 1e-07)
  Iteration 47 (of 50).   Current convergence value = 0.0001618 (threshold = 1e-07)
  Iteration 48 (of 50).   Current convergence value = 0.000161695 (threshold = 1e-07)
  Iteration 49 (of 50).   Current convergence value = 0.000161571 (threshold = 1e-07)
  Iteration 50 (of 50).   Current convergence value = 0.000161437 (threshold = 1e-07)
Current level = 2
  Iteration 1 (of 50).   Current convergence value = 0.000392126 (threshold = 1e-07)
  Iteration 2 (of 50).   Current convergence value = 0.000386467 (threshold = 1e-07)
  Iteration 3 (of 50).   Current convergence value = 0.00038131 (threshold = 1e-07)
  Iteration 4 (of 50).   Current convergence value = 0.000376658 (threshold = 1e-07)
  Iteration 5 (of 50).   Current convergence value = 0.000372321 (threshold = 1e-07)
  Iteration 6 (of 50).   Current convergence value = 0.000368349 (threshold = 1e-07)
  Iteration 7 (of 50).   Current convergence value = 0.000364589 (threshold = 1e-07)
  Iteration 8 (of 50).   Current convergence value = 0.000361172 (threshold = 1e-07)
  Iteration 9 (of 50).   Current convergence value = 0.000357932 (threshold = 1e-07)
  Iteration 10 (of 50).   Current convergence value = 0.000354765 (threshold = 1e-07)
  Iteration 11 (of 50).   Current convergence value = 0.000351997 (threshold = 1e-07)
  Iteration 12 (of 50).   Current convergence value = 0.000349178 (threshold = 1e-07)
  Iteration 13 (of 50).   Current convergence value = 0.000346434 (threshold = 1e-07)
  Iteration 14 (of 50).   Current convergence value = 0.000343781 (threshold = 1e-07)
  Iteration 15 (of 50).   Current convergence value = 0.000341215 (threshold = 1e-07)
  Iteration 16 (of 50).   Current convergence value = 0.000338913 (threshold = 1e-07)
  Iteration 17 (of 50).   Current convergence value = 0.000336595 (threshold = 1e-07)
  Iteration 18 (of 50).   Current convergence value = 0.000334385 (threshold = 1e-07)
  Iteration 19 (of 50).   Current convergence value = 0.000332281 (threshold = 1e-07)
  Iteration 20 (of 50).   Current convergence value = 0.000330136 (threshold = 1e-07)
  Iteration 21 (of 50).   Current convergence value = 0.000328145 (threshold = 1e-07)
  Iteration 22 (of 50).   Current convergence value = 0.000326041 (threshold = 1e-07)
  Iteration 23 (of 50).   Current convergence value = 0.000324014 (threshold = 1e-07)
  Iteration 24 (of 50).   Current convergence value = 0.000322037 (threshold = 1e-07)
  Iteration 25 (of 50).   Current convergence value = 0.000320137 (threshold = 1e-07)
  Iteration 26 (of 50).   Current convergence value = 0.000318197 (threshold = 1e-07)
  Iteration 27 (of 50).   Current convergence value = 0.000316221 (threshold = 1e-07)
  Iteration 28 (of 50).   Current convergence value = 0.000314323 (threshold = 1e-07)
  Iteration 29 (of 50).   Current convergence value = 0.000312342 (threshold = 1e-07)
  Iteration 30 (of 50).   Current convergence value = 0.000310367 (threshold = 1e-07)
  Iteration 31 (of 50).   Current convergence value = 0.000308531 (threshold = 1e-07)
  Iteration 32 (of 50).   Current convergence value = 0.000306533 (threshold = 1e-07)
  Iteration 33 (of 50).   Current convergence value = 0.000304734 (threshold = 1e-07)
  Iteration 34 (of 50).   Current convergence value = 0.000302763 (threshold = 1e-07)
  Iteration 35 (of 50).   Current convergence value = 0.000300927 (threshold = 1e-07)
  Iteration 36 (of 50).   Current convergence value = 0.000299018 (threshold = 1e-07)
  Iteration 37 (of 50).   Current convergence value = 0.000297152 (threshold = 1e-07)
  Iteration 38 (of 50).   Current convergence value = 0.000295324 (threshold = 1e-07)
  Iteration 39 (of 50).   Current convergence value = 0.000293401 (threshold = 1e-07)
  Iteration 40 (of 50).   Current convergence value = 0.000291651 (threshold = 1e-07)
  Iteration 41 (of 50).   Current convergence value = 0.00028986 (threshold = 1e-07)
  Iteration 42 (of 50).   Current convergence value = 0.000288172 (threshold = 1e-07)
  Iteration 43 (of 50).   Current convergence value = 0.000286338 (threshold = 1e-07)
  Iteration 44 (of 50).   Current convergence value = 0.000284567 (threshold = 1e-07)
  Iteration 45 (of 50).   Current convergence value = 0.000282876 (threshold = 1e-07)
  Iteration 46 (of 50).   Current convergence value = 0.000281259 (threshold = 1e-07)
  Iteration 47 (of 50).   Current convergence value = 0.000279537 (threshold = 1e-07)
  Iteration 48 (of 50).   Current convergence value = 0.000277779 (threshold = 1e-07)
  Iteration 49 (of 50).   Current convergence value = 0.000276155 (threshold = 1e-07)
  Iteration 50 (of 50).   Current convergence value = 0.00027451 (threshold = 1e-07)
Current level = 3
  Iteration 1 (of 50).   Current convergence value = 0.00117787 (threshold = 1e-07)
  Iteration 2 (of 50).   Current convergence value = 0.00104279 (threshold = 1e-07)
  Iteration 3 (of 50).   Current convergence value = 0.000932065 (threshold = 1e-07)
  Iteration 4 (of 50).   Current convergence value = 0.000837382 (threshold = 1e-07)
  Iteration 5 (of 50).   Current convergence value = 0.000758244 (threshold = 1e-07)
  Iteration 6 (of 50).   Current convergence value = 0.000691914 (threshold = 1e-07)
  Iteration 7 (of 50).   Current convergence value = 0.000635234 (threshold = 1e-07)
  Iteration 8 (of 50).   Current convergence value = 0.000586462 (threshold = 1e-07)
  Iteration 9 (of 50).   Current convergence value = 0.000544243 (threshold = 1e-07)
  Iteration 10 (of 50).   Current convergence value = 0.000507039 (threshold = 1e-07)
  Iteration 11 (of 50).   Current convergence value = 0.000474265 (threshold = 1e-07)
  Iteration 12 (of 50).   Current convergence value = 0.000445284 (threshold = 1e-07)
  Iteration 13 (of 50).   Current convergence value = 0.000419429 (threshold = 1e-07)
  Iteration 14 (of 50).   Current convergence value = 0.00039655 (threshold = 1e-07)
  Iteration 15 (of 50).   Current convergence value = 0.000376236 (threshold = 1e-07)
  Iteration 16 (of 50).   Current convergence value = 0.000358155 (threshold = 1e-07)
  Iteration 17 (of 50).   Current convergence value = 0.000342299 (threshold = 1e-07)
  Iteration 18 (of 50).   Current convergence value = 0.000328231 (threshold = 1e-07)
  Iteration 19 (of 50).   Current convergence value = 0.000316114 (threshold = 1e-07)
  Iteration 20 (of 50).   Current convergence value = 0.000305372 (threshold = 1e-07)
  Iteration 21 (of 50).   Current convergence value = 0.000295865 (threshold = 1e-07)
  Iteration 22 (of 50).   Current convergence value = 0.000287637 (threshold = 1e-07)
  Iteration 23 (of 50).   Current convergence value = 0.000280429 (threshold = 1e-07)
  Iteration 24 (of 50).   Current convergence value = 0.000274329 (threshold = 1e-07)
  Iteration 25 (of 50).   Current convergence value = 0.000268953 (threshold = 1e-07)
  Iteration 26 (of 50).   Current convergence value = 0.000264463 (threshold = 1e-07)
  Iteration 27 (of 50).   Current convergence value = 0.000260294 (threshold = 1e-07)
  Iteration 28 (of 50).   Current convergence value = 0.000256875 (threshold = 1e-07)
  Iteration 29 (of 50).   Current convergence value = 0.000254035 (threshold = 1e-07)
  Iteration 30 (of 50).   Current convergence value = 0.000251563 (threshold = 1e-07)
  Iteration 31 (of 50).   Current convergence value = 0.000249495 (threshold = 1e-07)
  Iteration 32 (of 50).   Current convergence value = 0.000247592 (threshold = 1e-07)
  Iteration 33 (of 50).   Current convergence value = 0.000246182 (threshold = 1e-07)
  Iteration 34 (of 50).   Current convergence value = 0.000244917 (threshold = 1e-07)
  Iteration 35 (of 50).   Current convergence value = 0.000243947 (threshold = 1e-07)
  Iteration 36 (of 50).   Current convergence value = 0.000243253 (threshold = 1e-07)
  Iteration 37 (of 50).   Current convergence value = 0.000242601 (threshold = 1e-07)
  Iteration 38 (of 50).   Current convergence value = 0.000242048 (threshold = 1e-07)
  Iteration 39 (of 50).   Current convergence value = 0.000241528 (threshold = 1e-07)
  Iteration 40 (of 50).   Current convergence value = 0.000241199 (threshold = 1e-07)
  Iteration 41 (of 50).   Current convergence value = 0.000241001 (threshold = 1e-07)
  Iteration 42 (of 50).   Current convergence value = 0.000240682 (threshold = 1e-07)
  Iteration 43 (of 50).   Current convergence value = 0.000240574 (threshold = 1e-07)
  Iteration 44 (of 50).   Current convergence value = 0.000240482 (threshold = 1e-07)
  Iteration 45 (of 50).   Current convergence value = 0.000240339 (threshold = 1e-07)
  Iteration 46 (of 50).   Current convergence value = 0.000240376 (threshold = 1e-07)
  Iteration 47 (of 50).   Current convergence value = 0.000240253 (threshold = 1e-07)
  Iteration 48 (of 50).   Current convergence value = 0.000240278 (threshold = 1e-07)
  Iteration 49 (of 50).   Current convergence value = 0.000240312 (threshold = 1e-07)
  Iteration 50 (of 50).   Current convergence value = 0.000240308 (threshold = 1e-07)
Current level = 4
  Iteration 1 (of 50).   Current convergence value = 0.001782 (threshold = 1e-07)
  Iteration 2 (of 50).   Current convergence value = 0.00156208 (threshold = 1e-07)
  Iteration 3 (of 50).   Current convergence value = 0.00139646 (threshold = 1e-07)
  Iteration 4 (of 50).   Current convergence value = 0.0012663 (threshold = 1e-07)
  Iteration 5 (of 50).   Current convergence value = 0.00115761 (threshold = 1e-07)
  Iteration 6 (of 50).   Current convergence value = 0.00106128 (threshold = 1e-07)
  Iteration 7 (of 50).   Current convergence value = 0.000977442 (threshold = 1e-07)
  Iteration 8 (of 50).   Current convergence value = 0.0009069 (threshold = 1e-07)
  Iteration 9 (of 50).   Current convergence value = 0.000854281 (threshold = 1e-07)
  Iteration 10 (of 50).   Current convergence value = 0.000824263 (threshold = 1e-07)
  Iteration 11 (of 50).   Current convergence value = 0.000798742 (threshold = 1e-07)
  Iteration 12 (of 50).   Current convergence value = 0.000774882 (threshold = 1e-07)
  Iteration 13 (of 50).   Current convergence value = 0.000753969 (threshold = 1e-07)
  Iteration 14 (of 50).   Current convergence value = 0.000734392 (threshold = 1e-07)
  Iteration 15 (of 50).   Current convergence value = 0.000716537 (threshold = 1e-07)
  Iteration 16 (of 50).   Current convergence value = 0.00067648 (threshold = 1e-07)
  Iteration 17 (of 50).   Current convergence value = 0.000629249 (threshold = 1e-07)
  Iteration 18 (of 50).   Current convergence value = 0.000587428 (threshold = 1e-07)
  Iteration 19 (of 50).   Current convergence value = 0.000550789 (threshold = 1e-07)
  Iteration 20 (of 50).   Current convergence value = 0.000518716 (threshold = 1e-07)
  Iteration 21 (of 50).   Current convergence value = 0.000490014 (threshold = 1e-07)
  Iteration 22 (of 50).   Current convergence value = 0.000468032 (threshold = 1e-07)
  Iteration 23 (of 50).   Current convergence value = 0.000452666 (threshold = 1e-07)
  Iteration 24 (of 50).   Current convergence value = 0.000438076 (threshold = 1e-07)
  Iteration 25 (of 50).   Current convergence value = 0.000424235 (threshold = 1e-07)
  Iteration 26 (of 50).   Current convergence value = 0.000410572 (threshold = 1e-07)
  Iteration 27 (of 50).   Current convergence value = 0.000397035 (threshold = 1e-07)
  Iteration 28 (of 50).   Current convergence value = 0.000384567 (threshold = 1e-07)
  Iteration 29 (of 50).   Current convergence value = 0.000372828 (threshold = 1e-07)
  Iteration 30 (of 50).   Current convergence value = 0.000361394 (threshold = 1e-07)
  Iteration 31 (of 50).   Current convergence value = 0.000350582 (threshold = 1e-07)
  Iteration 32 (of 50).   Current convergence value = 0.000340399 (threshold = 1e-07)
  Iteration 33 (of 50).   Current convergence value = 0.000330342 (threshold = 1e-07)
  Iteration 34 (of 50).   Current convergence value = 0.000321403 (threshold = 1e-07)
  Iteration 35 (of 50).   Current convergence value = 0.000313031 (threshold = 1e-07)
  Iteration 36 (of 50).   Current convergence value = 0.000305041 (threshold = 1e-07)
  Iteration 37 (of 50).   Current convergence value = 0.000297436 (threshold = 1e-07)
  Iteration 38 (of 50).   Current convergence value = 0.000290269 (threshold = 1e-07)
  Iteration 39 (of 50).   Current convergence value = 0.000283651 (threshold = 1e-07)
  Iteration 40 (of 50).   Current convergence value = 0.000277123 (threshold = 1e-07)
  Iteration 41 (of 50).   Current convergence value = 0.000270909 (threshold = 1e-07)
  Iteration 42 (of 50).   Current convergence value = 0.000265105 (threshold = 1e-07)
  Iteration 43 (of 50).   Current convergence value = 0.00025969 (threshold = 1e-07)
  Iteration 44 (of 50).   Current convergence value = 0.000254835 (threshold = 1e-07)
  Iteration 45 (of 50).   Current convergence value = 0.0002524 (threshold = 1e-07)
  Iteration 46 (of 50).   Current convergence value = 0.00025043 (threshold = 1e-07)
  Iteration 47 (of 50).   Current convergence value = 0.000248814 (threshold = 1e-07)
  Iteration 48 (of 50).   Current convergence value = 0.000246734 (threshold = 1e-07)
  Iteration 49 (of 50).   Current convergence value = 0.000244866 (threshold = 1e-07)
  Iteration 50 (of 50).   Current convergence value = 0.0002433 (threshold = 1e-07)
   N4BiasFieldCorrectionImageFilter (0x7f8b3d0339d0)
     RTTI typeinfo:   itk::N4BiasFieldCorrectionImageFilter<itk::Image<float, 3u>, itk::Image<float, 3u>, itk::Image<float, 3u> >
     Reference Count: 1
     Modified Time: 232600
     Debug: Off
     Object Name: 
     Observers: 
       IterationEvent(Command)
     Inputs: 
       ConfidenceImage: (0x0)
       MaskImage: (0x7f8b3d028060)
       Primary: (0x7f8b3d027c40) *
     Indexed Inputs: 
       0: Primary (0x7f8b3d027c40)
       1: MaskImage (0x7f8b3d028060)
       2: ConfidenceImage (0x0)
     Required Input Names: Primary
     NumberOfRequiredInputs: 1
     Outputs: 
       Primary: (0x7f8b3d0342b0)
     Indexed Outputs: 
       0: Primary (0x7f8b3d0342b0)
     NumberOfRequiredOutputs: 1
     Number Of Work Units: 16
     ReleaseDataFlag: Off
     ReleaseDataBeforeUpdateFlag: Off
     AbortGenerateData: Off
     Progress: 0
     Multithreader: 
       RTTI typeinfo:   itk::PoolMultiThreader
       Reference Count: 1
       Modified Time: 232339
       Debug: Off
       Object Name: 
       Observers: 
         none
       Number of Work Units: 16
       Number of Threads: 4
       Global Maximum Number Of Threads: 128
       Global Default Number Of Threads: 4
       Global Default Threader Type: PoolMultiThreader
       SingleMethod: 0
       SingleData: 0x0
     DynamicMultiThreading: On
     CoordinateTolerance: 1e-06
     DirectionTolerance: 1e-06
     Use Mask Label: 0
     Mask label: 1
     Number of histogram bins: 200
     Wiener filter noise: 0.01
     Bias field FWHM: 0.15
     Maximum number of iterations: [50, 50, 50, 50]
     Convergence threshold: 1e-07
     Spline order: 3
     Number of fitting levels: [4, 4, 4]
     Number of control points: [4, 5, 4]
     CurrentConvergenceMeasurement: 0.0002433
     CurrentLevel: 4
     ElapsedIterations: 51
     LogBiasFieldControlPointLattice: 
       Image (0x7f8b3d02bd70)
         RTTI typeinfo:   itk::Image<itk::Vector<float, 1u>, 3u>
         Reference Count: 1
         Modified Time: 461775
         Debug: Off
         Object Name: 
         Observers: 
           none
         Source: (none)
         Source output name: (none)
         Release Data: Off
         Data Released: False
         Global Release Data: Off
         PipelineMTime: 0
         UpdateMTime: 0
         RealTimeStamp: 0 seconds 
         LargestPossibleRegion: 
           Dimension: 3
           Index: [0, 0, 0]
           Size: [11, 19, 11]
         BufferedRegion: 
           Dimension: 3
           Index: [0, 0, 0]
           Size: [11, 19, 11]
         RequestedRegion: 
           Dimension: 3
           Index: [0, 0, 0]
           Size: [11, 19, 11]
         Spacing: [24.5999, 24.8438, 24.375]
         Origin: [-24.5999, 27.6585, -21.1272]
         Direction: 
1 3.7561e-08 2.32353e-09
3.7561e-08 -0.992376 -0.123249
2.32353e-09 -0.123249 0.992376

         IndexToPointMatrix: 
24.5999 9.33155e-07 5.6636e-08
9.23997e-07 -24.6543 -3.00419
5.71586e-08 -3.06196 24.1892

         PointToIndexMatrix: 
0.0406505 1.52687e-09 9.44525e-11
1.51189e-09 -0.0399447 -0.00496095
9.53242e-11 -0.00505636 0.0407129

         Inverse Direction: 
1 3.7561e-08 2.32353e-09
3.7561e-08 -0.992376 -0.123249
2.32353e-09 -0.123249 0.992376

         PixelContainer: 
           ImportImageContainer (0x7f8b3d02c020)
             RTTI typeinfo:   itk::ImportImageContainer<unsigned long, itk::Vector<float, 1u> >
             Reference Count: 1
             Modified Time: 461776
             Debug: Off
             Object Name: 
             Observers: 
               none
             Pointer: 0x7f8b39d05e00
             Container manages memory: true
             Size: 2299
             Capacity: 2299
Elapsed time: 12.0611

 *** caught segfault ***
address 0x12b97f400, cause 'memory not mapped'

Traceback:
 1: .helpn4BiasFieldCorrection(args)
 2: n4BiasFieldCorrection(masked_image, mask = mask, verbose = TRUE)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace
Selection: 2020-01-16 15:22:08.189 R[23012:3370064] WARNING: nextEventMatchingMask should only be called from the Main Thread! This will throw an exception in the future.

Created on 2020-01-16 by the reprex package (v0.3.0.9001)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.0 (2019-04-26)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2020-01-16                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version     date       lib source                             
#>  abind         1.4-5       2016-07-21 [1] CRAN (R 3.6.0)                     
#>  ANTsR         0.5.4.2     2019-11-14 [1] local                              
#>  ANTsRCore   * 0.7.3       2019-12-11 [1] Github (ANTsX/ANTsRCore@416d8f1)   
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 3.6.0)                     
#>  backports     1.1.5       2019-10-02 [1] CRAN (R 3.6.0)                     
#>  bitops        1.0-6       2013-08-17 [1] CRAN (R 3.6.0)                     
#>  cli           2.0.0       2019-12-09 [1] CRAN (R 3.6.0)                     
#>  colorout    * 1.2-1       2019-06-19 [1] Github (jalvesaq/colorout@7ea9440) 
#>  crayon        1.3.4       2017-09-16 [1] CRAN (R 3.6.0)                     
#>  curl          4.3         2019-12-02 [1] CRAN (R 3.6.0)                     
#>  digest        0.6.23      2019-11-23 [1] CRAN (R 3.6.0)                     
#>  evaluate      0.14        2019-05-28 [1] CRAN (R 3.6.0)                     
#>  extrantsr   * 3.9.12.1    2020-01-03 [1] local                              
#>  fansi         0.4.0       2018-10-05 [1] CRAN (R 3.6.0)                     
#>  fs            1.3.1       2019-05-06 [1] CRAN (R 3.6.0)                     
#>  fslr          2.24.4      2019-12-03 [1] local                              
#>  glue          1.3.1       2019-03-12 [1] CRAN (R 3.6.0)                     
#>  highr         0.8         2019-03-20 [1] CRAN (R 3.6.0)                     
#>  htmltools     0.4.0       2019-10-04 [1] CRAN (R 3.6.0)                     
#>  httr          1.4.1       2019-08-05 [1] CRAN (R 3.6.0)                     
#>  ITKR          0.5.2       2019-11-05 [1] Github (stnava/ITKR@fe97efa)       
#>  knitr         1.26.1      2020-01-05 [1] Github (muschellij2/knitr@f5af631) 
#>  lattice       0.20-38     2018-11-04 [1] CRAN (R 3.6.0)                     
#>  magrittr      1.5         2014-11-22 [1] CRAN (R 3.6.0)                     
#>  Matrix        1.2-17      2019-03-22 [1] CRAN (R 3.6.0)                     
#>  matrixStats   0.55.0      2019-09-07 [1] CRAN (R 3.6.0)                     
#>  mgcv          1.8-28      2019-03-21 [1] CRAN (R 3.6.0)                     
#>  mime          0.8         2019-12-19 [1] CRAN (R 3.6.0)                     
#>  neurobase   * 1.29.1      2020-01-14 [1] local                              
#>  nlme          3.1-141     2019-08-01 [1] CRAN (R 3.6.0)                     
#>  oro.nifti   * 0.10.1      2019-12-14 [1] local                              
#>  pillar        1.4.3       2019-12-20 [1] CRAN (R 3.6.0)                     
#>  pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 3.6.0)                     
#>  plyr          1.8.5       2019-12-10 [1] CRAN (R 3.6.0)                     
#>  purrr         0.3.3       2019-10-18 [1] CRAN (R 3.6.0)                     
#>  R.matlab      3.6.2       2018-09-27 [1] CRAN (R 3.6.0)                     
#>  R.methodsS3   1.7.1       2016-02-16 [1] CRAN (R 3.6.0)                     
#>  R.oo          1.23.0      2019-11-03 [1] CRAN (R 3.6.0)                     
#>  R.utils       2.9.0       2019-06-13 [1] CRAN (R 3.6.0)                     
#>  R6            2.4.1       2019-11-12 [1] CRAN (R 3.6.0)                     
#>  Rcpp          1.0.3       2019-11-08 [1] CRAN (R 3.6.0)                     
#>  RcppEigen     0.3.3.7.0   2019-11-16 [1] CRAN (R 3.6.0)                     
#>  reprex        0.3.0.9001  2020-01-05 [1] Github (tidyverse/reprex@5ae0b29)  
#>  rlang         0.4.2       2019-11-23 [1] CRAN (R 3.6.0)                     
#>  rmarkdown     2.0         2019-12-12 [1] CRAN (R 3.6.0)                     
#>  RNifti        1.0.1       2019-11-27 [1] CRAN (R 3.6.0)                     
#>  rstudioapi    0.10.0-9003 2020-01-05 [1] Github (rstudio/rstudioapi@abe596d)
#>  sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 3.6.0)                     
#>  stapler       0.7.0.1     2020-01-03 [1] local                              
#>  stringi       1.4.3       2019-03-12 [1] CRAN (R 3.6.0)                     
#>  stringr       1.4.0       2019-02-10 [1] CRAN (R 3.6.0)                     
#>  styler        1.1.1       2019-05-06 [1] CRAN (R 3.6.0)                     
#>  tibble        2.1.3       2019-06-06 [1] CRAN (R 3.6.0)                     
#>  WhiteStripe   2.3.2       2019-10-01 [1] local                              
#>  withr         2.1.2       2018-03-15 [1] CRAN (R 3.6.0)                     
#>  xfun          0.11        2019-11-12 [1] CRAN (R 3.6.0)                     
#>  xml2          1.2.2       2019-08-09 [1] CRAN (R 3.6.0)                     
#>  yaml          2.2.0       2018-07-25 [1] CRAN (R 3.6.0)                     
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

@muschellij2
Copy link
Collaborator Author

Still getting N4 fails with 3D: https://travis-ci.org/muschellij2/ANTsRCore/jobs/645574697#L4437

@stnava
Copy link
Member

stnava commented Feb 3, 2020 via email

@ntustison
Copy link
Member

Looking at this now. I wonder if our previous attempt at eliminating a similar ANTsPy occurrence is now causing this.

@stnava
Copy link
Member

stnava commented Feb 4, 2020 via email

@ntustison
Copy link
Member

Yeah, that's the next thing I was going to try as soon as I can reproduce it on the dgx. Apparently my ANTsRCore was older so I wasn't able to reproduce.

@stnava
Copy link
Member

stnava commented Feb 4, 2020 via email

@ntustison
Copy link
Member

Hey @stnava , I'm having trouble reproducing so, as you suggested, can you just go change line 144 from int to float in your local ANTsRCore copy and see if that works for you? If so, then I'll change it in ANTs and hopefully we won't have any more problems with this.

@muschellij2
Copy link
Collaborator Author

Is this resolved? I am still getting segfaults (which is a pretty strong fail - are there possibilities to make this robust in R to fail but not segfault?). I'm reinstalling everything from scratch today (ITKR and ANTsRCore), and retry

@ntustison
Copy link
Member

@muschellij2 --- can you try the small change I recommended to Brian in the comment previous to yours? My guess is that this change should fix it but I'm unable to reproduce the segfault on my end.

@stnava
Copy link
Member

stnava commented Feb 20, 2020

@ntustison - I tried that change but still got issues .... however, I would like to know if anyone else can reproduce. my finding was that the error only occurred (on my example data) if I both used a mask and downsampling >2. the error occurs after the filter completes (judging by verbose mode).

@ntustison
Copy link
Member

Awesome. Thanks. Too bad it didn't work, though.

There's not an insignificant amount of processing that takes place after the verbose output which is why I really need to be able to reproduce before I can debug. I'll take a look at this again today and see if somehow things are different this time around.

@muschellij2
Copy link
Collaborator Author

muschellij2 commented Feb 20, 2020

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
dims = c(50, 50)
img<-makeImage(imagesize = dims, rnorm(prod(dims)) )
n4img<-n4BiasFieldCorrection(img)
n4img<-n4BiasFieldCorrection(img, mask = img > 0)
testthat::expect_error(n4BiasFieldCorrection(img, weight_mask = "somepath"))
testthat::expect_error(n4BiasFieldCorrection(img, splineParam = rep(200, 3)))
n4img<-n4BiasFieldCorrection(img, splineParam = c(200, 20))

rm(img); gc()
#>           used (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1860508 99.4    3551941 189.7         NA  2702461 144.4
#> Vcells 3323055 25.4    8388608  64.0      16384  5520418  42.2
rm(n4img); gc()
#>           used (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1860765 99.4    3551941 189.7         NA  2702461 144.4
#> Vcells 3323714 25.4    8388608  64.0      16384  5520418  42.2
fname = getANTsRData("r16")
in_img = antsImageRead(fname)
n4 = n4BiasFieldCorrection(in_img)
rm(n4); gc()
#>           used (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1863010 99.5    3551941 189.7         NA  2702461 144.4
#> Vcells 3329119 25.4    8388608  64.0      16384  5520418  42.2
mask = in_img > 0
mask2 = antsImageClone(mask, out_pixeltype = "float")
# fails

Created on 2020-02-20 by the reprex package (v0.3.0.9001)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2020-02-20                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version     date       lib source                             
#>  ANTsRCore   * 0.7.3       2020-02-20 [1] Github (ANTsX/ANTsRCore@6f7e9aa)   
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 3.6.0)                     
#>  backports     1.1.5       2019-10-02 [1] CRAN (R 3.6.0)                     
#>  cli           2.0.1       2020-01-08 [1] CRAN (R 3.6.0)                     
#>  crayon        1.3.4       2017-09-16 [1] CRAN (R 3.6.0)                     
#>  digest        0.6.23      2019-11-23 [1] CRAN (R 3.6.0)                     
#>  ellipsis      0.3.0       2019-09-20 [1] CRAN (R 3.6.0)                     
#>  evaluate      0.14        2019-05-28 [1] CRAN (R 3.6.0)                     
#>  fansi         0.4.1       2020-01-08 [1] CRAN (R 3.6.0)                     
#>  fs            1.3.1       2019-05-06 [1] CRAN (R 3.6.0)                     
#>  glue          1.3.1       2019-03-12 [1] CRAN (R 3.6.0)                     
#>  highr         0.8         2019-03-20 [1] CRAN (R 3.6.0)                     
#>  htmltools     0.4.0       2019-10-04 [1] CRAN (R 3.6.0)                     
#>  ITKR          0.5.2       2020-02-20 [1] Github (stnava/ITKR@fe97efa)       
#>  knitr         1.26.1      2020-01-05 [1] Github (muschellij2/knitr@f5af631) 
#>  lattice       0.20-38     2018-11-04 [1] CRAN (R 3.6.2)                     
#>  magrittr      1.5         2014-11-22 [1] CRAN (R 3.6.0)                     
#>  Matrix        1.2-18      2019-11-27 [1] CRAN (R 3.6.2)                     
#>  pillar        1.4.3       2019-12-20 [1] CRAN (R 3.6.0)                     
#>  pkgconfig     2.0.3       2019-09-22 [1] CRAN (R 3.6.0)                     
#>  purrr         0.3.3       2019-10-18 [1] CRAN (R 3.6.0)                     
#>  R6            2.4.1       2019-11-12 [1] CRAN (R 3.6.0)                     
#>  Rcpp          1.0.3       2019-11-08 [1] CRAN (R 3.6.2)                     
#>  RcppEigen     0.3.3.7.0   2019-11-16 [1] CRAN (R 3.6.0)                     
#>  reprex        0.3.0.9001  2020-01-05 [1] Github (tidyverse/reprex@5ae0b29)  
#>  rlang         0.4.4       2020-01-28 [1] CRAN (R 3.6.0)                     
#>  rmarkdown     2.0.7       2020-01-17 [1] Github (rstudio/rmarkdown@2faf16a) 
#>  rstudioapi    0.11.0-9000 2020-02-19 [1] Github (rstudio/rstudioapi@deb9c47)
#>  sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 3.6.0)                     
#>  stringi       1.4.5       2020-01-11 [1] CRAN (R 3.6.0)                     
#>  stringr       1.4.0       2019-02-10 [1] CRAN (R 3.6.0)                     
#>  styler        1.1.1       2019-05-06 [1] CRAN (R 3.6.0)                     
#>  testthat      2.3.0       2019-11-05 [1] CRAN (R 3.6.0)                     
#>  tibble        2.1.3       2019-06-06 [1] CRAN (R 3.6.0)                     
#>  withr         2.1.2       2018-03-15 [1] CRAN (R 3.6.0)                     
#>  xfun          0.11        2019-11-12 [1] CRAN (R 3.6.0)                     
#>  yaml          2.2.0       2018-07-25 [1] CRAN (R 3.6.0)                     
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
> n4 = n4BiasFieldCorrection(in_img, mask = mask, verbose = TRUE)

This seems to fail even before N4 starts:

Running N4 for 2-dimensional images.


 *** caught segfault ***

 *** caught segfault ***
address 0x109bc1000, cause 'memory not mapped'
address 0x109bc5000, cause 'memory not mapped'

Traceback:

Traceback:
 1:  1: .helpn4BiasFieldCorrection(args).helpn4BiasFieldCorrection(args)

 2:  2: n4BiasFieldCorrection(in_img, mask = mask, verbose = TRUE)n4BiasFieldCorrection(in_img, mask = mask, verbose = TRUE)


Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

@stnava
Copy link
Member

stnava commented Feb 20, 2020 via email

@ntustison
Copy link
Member

Yeah, that example works for me, too. @stnava --- it looks I don't have permission to access that example. Can you open up the permissions?

@stnava
Copy link
Member

stnava commented Feb 20, 2020 via email

@ntustison
Copy link
Member

Thanks @stnava . This ("/usr/bin/R") was particularly useful. I installed a different version of R on the DGX which is why I haven't been able to reproduce. So, in summary, for me

  • R --version = 3.5.1 ---> passes
  • /usr/bin/R --version = 3.6.1 ---> segfaults

I assume both you and @muschellij2 are using 3.6.1, correct?

@stnava
Copy link
Member

stnava commented Feb 20, 2020 via email

@muschellij2
Copy link
Collaborator Author

muschellij2 commented Feb 20, 2020 via email

@ntustison
Copy link
Member

#112

ntustison referenced this issue in ANTsX/ANTs Feb 21, 2020
@muschellij2
Copy link
Collaborator Author

Same behavior - still failing if both sets of code are run, but not failing if first set of code is not run.

ib> sessioninfo::session_info("ANTsRCore")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       macOS Mojave 10.14.6        
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-03-23                  

─ Packages ───────────────────────────────────────────────────────────────────
 package   * version   date       lib source                          
 ANTsRCore   0.7.3.1   2020-03-23 [1] Github (ANTsX/ANTsRCore@074e8cd)
 ITKR        0.5.3     2020-03-18 [1] Github (stnava/ITKR@715331d)    
 lattice     0.20-38   2018-11-04 [1] CRAN (R 3.6.3)                  
 magrittr    1.5       2014-11-22 [1] CRAN (R 3.6.0)                  
 Matrix      1.2-18    2019-11-27 [1] CRAN (R 3.6.3)                  
 Rcpp        1.0.4.3   2020-03-20 [1] Github (RcppCore/Rcpp@ce5538b)  
 RcppEigen   0.3.3.7.0 2019-11-16 [1] CRAN (R 3.6.0)                  

[1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library

@ntustison
Copy link
Member

That's unfortunate. I was able to reproduce the issue on my laptop if I went the git clone ..; R CMD INSTALL ANTsRCore installation route vs. devtools::install_github(). And what I corrected seemed to fit this bizarre behavior and it certainly fixed it on my laptop. At this point, I don't know what else to do.

@muschellij2
Copy link
Collaborator Author

muschellij2 commented Mar 23, 2020 via email

@ntustison
Copy link
Member

Okay, if that doesn't work, one thing we can do is some simplistic, yet effective, debugging by placing std::cout statements in N4BiasFieldCorrection.cxx before compilation and seeing where it segfaults.

@muschellij2
Copy link
Collaborator Author

Still failing - It's a bit complicated to change what is being used, need to change ANTs tag, then make sure ANTs tag really matches up with ITKR ITK tag. So if that can be merged after testing, it may be easier to set this.

@muschellij2
Copy link
Collaborator Author

Not sure what to do here?

@ntustison
Copy link
Member

I'm sorry but I don't either at this point. Originally, there was an obvious memory problem with one of the image iterators which definitely needed to be fixed. After fixing that, there was an additional memory issue (or, I would say, idiosyncrasy with specific platforms) that I was able to reproduce on only one of my machines through a specific build process. After modifying that, I can no longer reproduce the issue on any machine to which I have access.

Are you able to reproduce this on more than one machine? And, if so, can you isolate the section of code through the use of std::cout statements so I have some idea where this memory issue is surfacing?

@muschellij2
Copy link
Collaborator Author

muschellij2 commented Mar 30, 2020 via email

@muschellij2
Copy link
Collaborator Author

@ntustison
Copy link
Member

Why do you need to modify N4BiasFieldCorrection.cxx in the ANTsX GitHub repo? When I discovered the second issue, I was only modifying it locally in the ANTsRCore directory.

$ pwd
/Users/ntustison/Pkg/ANTsRCore
$ find . -name N4BiasFieldCorrection.cxx
./inst/include/N4BiasFieldCorrection.cxx
./src/ants/Examples/N4BiasFieldCorrection.cxx

@ntustison
Copy link
Member

But, I agree, it's a burden. However, I don't what else to tell you as I can't reproduce it. It would be nice if there were another machine that could verify that this isn't an isolated issue.

@muschellij2
Copy link
Collaborator Author

OK - added the lines and seems to segfault at: https://github.com/muschellij2/ANTs/blob/master/Examples/N4BiasFieldCorrection.cxx#L147

> n4 = n4BiasFieldCorrection(in_img, mask = mask, verbose = TRUE)

Creating ImageType

Creating MaskImageType

Running N4 for 2-dimensional images.


Getting CorrecterType

Reading in Input Image

maskImageOption: Option (0x7faae9e1cb40)
  RTTI typeinfo:   itk::ants::CommandLineOption
  Reference Count: 3
  Modified Time: 739716
  Debug: Off
  Object Name: 
  Observers: 
    none
  Source: (none)
  Source output name: (none)
  Release Data: Off
  Data Released: False
  Global Release Data: Off
  PipelineMTime: 0
  UpdateMTime: 0
  RealTimeStamp: 0 seconds 


Reading in Mask file: 0x7faae9a604b0

 *** caught segfault ***

@ntustison
Copy link
Member

Interesting. So it seems there's something going on that the reader doesn't like in this particular spot of this particular program. Not surprising as there's a bit of pointer stuff going on there. Before we dive into that, perhaps you should try replacing the ReadImageData call with the more ITK direct substitute. Specifically, comment out line 147 of your code and add the following:

using MaskImageReaderType = itk::ImageFileReader<MaskImageType>;
typename MaskImageReaderType::Pointer maskImageReader = MaskImageReaderType::New();
maskImageReader->SetFileName( inputMaskFile.c_str() );
maskImage = maskImageReader->GetOutput();
maskImage->Update();
maskImage->DisconnectPipeline();

@muschellij2
Copy link
Collaborator Author

Seems to have broken all the code:

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
 dims = c(50, 50)
 img<-makeImage(imagesize = dims, rnorm(prod(dims)) )
 n4img<-n4BiasFieldCorrection(img)
 n4img<-n4BiasFieldCorrection(img, mask = img > 0)
#> /Library/Frameworks/R.framework/Versions/3.6/Resources/library/ITKR/libs/include/ITK-5.1/itkImageFileReader.hxx:133:
#>  Could not create IO object for reading file 0x7f88c286b610
#> The file doesn't exist. 
#> Filename = 0x7f88c286b610
 testthat::expect_error(n4BiasFieldCorrection(img, weight_mask = "somepath"))
 testthat::expect_error(n4BiasFieldCorrection(img, splineParam = rep(200, 3)))
 n4img<-n4BiasFieldCorrection(img, splineParam = c(200, 20))

 rm(img); gc()
#>           used  (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1893705 101.2    3797505 202.9         NA  2451542 131.0
#> Vcells 3381342  25.8    8388608  64.0      16384  4749362  36.3
 rm(n4img); gc()
#>           used  (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1893944 101.2    3797505 202.9         NA  2451542 131.0
#> Vcells 3381972  25.9    8388608  64.0      16384  4749362  36.3
fname = getANTsRData("r16")
in_img = antsImageRead(fname)
n4 = n4BiasFieldCorrection(in_img)
rm(n4); gc()
#>           used  (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1897591 101.4    3797505 202.9         NA  2451542 131.0
#> Vcells 3390396  25.9    8388608  64.0      16384  4749362  36.3
mask = in_img > 0
mask2 = antsImageClone(mask, out_pixeltype = "float")
# fails
mask
#> antsImage
#>   Pixel Type          : unsigned char 
#>   Components Per Pixel: 1 
#>   Dimensions          : 256x256 
#>   Voxel Spacing       : 1x1 
#>   Origin              : 0 0 
#>   Direction           : 1 0 0 1
sum(mask)
#> [1] 19278
n4 = n4BiasFieldCorrection(in_img, mask = mask, verbose = TRUE)
#> /Library/Frameworks/R.framework/Versions/3.6/Resources/library/ITKR/libs/include/ITK-5.1/itkImageFileReader.hxx:133:
#>  Could not create IO object for reading file 0x7f88bde1f0f0
#> The file doesn't exist. 
#> Filename = 0x7f88bde1f0f0
# fails

Created on 2020-03-30 by the reprex package (v0.3.0.9001)

@ntustison
Copy link
Member

Sorry, my bad---I forgot that all the R overhead was added to the ANTs Read/Write functionality.

So I don't know what to do at this point. As far as I can tell, it doesn't look to be anything specific to the N4 program. If I could reproduce the error, then I would see where it's segfaulting in the reader or switch the order of reading in the input image vs. the mask. But all this would be just trial-and-error and certainly not something that can be done in a GitHub repo thread.

@muschellij2
Copy link
Collaborator Author

FYI - we're trying using GitHub actions to compile/test the packages and found that this still is lurking:

https://github.com/muschellij2/ANTsRCore/runs/832316950?check_suite_focus=true#step:9:185

@ntustison
Copy link
Member

Thanks @muschellij2 .

I would assume the seg faulting is the same as before in that it occurs when the mask is read using the ReadImage function. However, I still can't reproduce this error on any of my machines or any of the machines to which I have access. Maybe it has something to do with this comment here but without being able to reproduce, I can't diagnose properly or provide a possible fix. Perhaps you can try to pinpoint the precise line in that function where it seg faults.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants