ANTsX / ANTsRCore

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

BUG: Address N4 mask issue. #112

Closed ntustison closed 4 years ago

ntustison commented 4 years ago

I think this should do it.

ntustison commented 4 years ago

@stnava , @muschellij2 --- it looks like the failures stem from this ANTs pull request due to a switch in ITK from using Type to Enum. I'm assuming this will have to be updated in ITKR. Do you want me to do that?

muschellij2 commented 4 years ago

I would appreciate it as I would not know how to best address this. I don't know if @stnava has any concerns of breaks? Best, John

On Fri, Feb 21, 2020 at 10:21 AM Nick Tustison notifications@github.com wrote:

@stnava https://github.com/stnava , @muschellij2 https://github.com/muschellij2 --- it looks like the failures stem from this ANTs pull request https://github.com/ANTsX/ANTs/commit/76b0cc051bc0edefc9c638969264d23f36bdce6c due to a switch in ITK from using Type to Enum. I'm assuming this will have to be updated in ITKR. Do you want me to do that?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsRCore/pull/112?email_source=notifications&email_token=AAIGPLQQROUULMCJRSWBZVLRD7WOBA5CNFSM4KY3KLVKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMTBMMY#issuecomment-589698611, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLSFR4R4BF3EXZW2RHTRD7WOBANCNFSM4KY3KLVA .

ntustison commented 4 years ago

Thanks @muschellij2 . I don't have much familiarity with pull requests for ITKR so I'll wait until @stnava weighs in.

muschellij2 commented 4 years ago

Got it - I misunderstood. Do you want me to change the ITK tag in ITKR to bcd762e7bd743341c2dce84852ced49d8292f293 ? Best, John

On Fri, Feb 21, 2020 at 10:47 AM Nick Tustison notifications@github.com wrote:

Thanks @muschellij2 https://github.com/muschellij2 . I don't have much familiarity with pull requests for ITKR so I'll wait until @stnava https://github.com/stnava weighs in.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsRCore/pull/112?email_source=notifications&email_token=AAIGPLXA3RV352RPP7OQZATRD7ZPRA5CNFSM4KY3KLVKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEMTEHNY#issuecomment-589710263, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLWYCIEXCZVH4KUMBSTRD7ZPRANCNFSM4KY3KLVA .

muschellij2 commented 4 years ago

Also you need to re-document as the NAMESPACE was deleted.

ntustison commented 4 years ago

@muschellij2 Sorry about that---yeah, if you could clean up the mess I created, that would be great.

ntustison commented 4 years ago

Hey @muschellij2 and @stnava --- getting back to the N4 segfault issue. @stnava merged the updated ITKR with the new ITK git tag. You can see that the travis builds are still failing due to the same issue. Is there someplace in ANTsRCore where I need to update the ITKR git tag?

muschellij2 commented 4 years ago

I don't think it is needed, we can close Best, John

On Wed, Mar 18, 2020 at 2:26 PM Nick Tustison notifications@github.com wrote:

@ntustison commented on this pull request.

In NAMESPACE https://github.com/ANTsX/ANTsRCore/pull/112#discussion_r394554900:

@@ -1,224 +1,5 @@

Generated by roxygen2: do not edit by hand

-S3method(apply,antsImage)

Sure. I have no idea why these lines were deleted or even if this pull is still needed.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsRCore/pull/112#discussion_r394554900, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLW6GFPM5XKAEZM253DRIEG5ZANCNFSM4KY3KLVA .

muschellij2 commented 4 years ago

This still segfaults for me after fresh install yesterday. I can't do a reprex because it segfaults:

sessioninfo::session_info("ANTsRCore")
─ 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-03-19                  

─ Packages ───────────────────────────────────────────────────────────────────
 package   * version   date       lib source                          
 ANTsRCore   0.7.3.1   2020-03-19 [1] Github (ANTsX/ANTsRCore@5749f7c)
 ITKR        0.5.3     2020-03-18 [1] Github (stnava/ITKR@715331d)    
 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)                  
 Rcpp        1.0.4.2   2020-03-19 [1] Github (RcppCore/Rcpp@20e462f)  
 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

https://travis-ci.org/github/ANTsX/ANTsRCore/jobs/662685464

ntustison commented 4 years ago

Is this still the example that faults?

fname = getANTsRData("r16")
in_img = antsImageRead(fname)
full_image = in_img > -1
n4 = n4BiasFieldCorrection(in_img, mask = full_image)

If it does, I just updated and ran it on two of my macs and our dgx. I can't reproduce.

On the DGX:

> fname = getANTsRData("r16")
> in_img = antsImageRead(fname)
> full_image = in_img > -1
> n4 = n4BiasFieldCorrection(in_img, mask = full_image)
> sessioninfo::session_info( "ANTsRCore" )
─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.1 (2019-07-05)
 os       Ubuntu 18.04.2 LTS          
 system   x86_64, linux-gnu           
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/New_York            
 date     2020-03-19                  

─ Packages ───────────────────────────────────────────────────────────────────
 package   * version   date       lib source                          
 ANTsRCore * 0.7.3.1   2020-03-19 [1] Github (ANTsX/ANTsRCore@5749f7c)
 ITKR        0.5.3     2020-03-19 [1] Github (stnava/ITKR@715331d)    
 lattice     0.20-38   2018-11-04 [4] CRAN (R 3.6.1)                  
 magrittr    1.5       2014-11-22 [2] CRAN (R 3.6.1)                  
 Matrix      1.2-17    2019-03-22 [4] CRAN (R 3.6.1)                  
 Rcpp        1.0.4     2020-03-17 [1] CRAN (R 3.6.1)                  
 RcppEigen   0.3.3.7.0 2019-11-16 [1] CRAN (R 3.6.1)                  

[1] /home/ntustison/R/x86_64-pc-linux-gnu-library/3.6
[2] /usr/local/lib/R/site-library
[3] /usr/lib/R/site-library
[4] /usr/lib/R/library

On my newish iMac:

> fname = getANTsRData("r16")
> in_img = antsImageRead(fname)
> full_image = in_img > -1
> n4 = n4BiasFieldCorrection(in_img, mask = full_image)
> sessioninfo::session_info( "ANTsRCore" )
─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.2 (2019-12-12)
 os       macOS Catalina 10.15.3      
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2020-03-19                  

─ Packages ───────────────────────────────────────────────────────────────────
 package   * version   date       lib source                          
 ANTsRCore * 0.7.3.1   2020-03-19 [1] Github (ANTsX/ANTsRCore@5749f7c)
 ITKR        0.5.3     2020-03-19 [1] Github (stnava/ITKR@715331d)    
 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)                  
 Rcpp        1.0.4.3   2020-03-19 [1] Github (RcppCore/Rcpp@5ea44f1)  
 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

Perhaps @stnava can see if it fails for him

ntustison commented 4 years ago

Since you're running Mojave, I'll also include my older iMac (running High Sierra):

> fname = getANTsRData("r16")
> in_img = antsImageRead(fname)
> full_image = in_img > -1
> n4 = n4BiasFieldCorrection(in_img, mask = full_image)
> sessioninfo::session_info( "ANTsRCore" )
─ Session info ───────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 3.6.3 (2020-02-29)
 os       macOS High Sierra 10.13.6   
 system   x86_64, darwin15.6.0        
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2020-03-19                  

─ Packages ───────────────────────────────────────────────────────────────────
 package   * version   date       lib source                          
 ANTsRCore * 0.7.3.1   2020-03-19 [1] Github (ANTsX/ANTsRCore@5749f7c)
 ITKR        0.5.3     2020-03-19 [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-19 [1] Github (RcppCore/Rcpp@5ea44f1)  
 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
muschellij2 commented 4 years ago

That works fine for me.

This one fails (only difference is the mask > 0 vs. > -1): fname = getANTsRData("r16") in_img = antsImageRead(fname) mask = in_img > 0 n4 = n4BiasFieldCorrection(in_img, mask = mask)

Best, John

On Thu, Mar 19, 2020 at 7:36 PM Nick Tustison notifications@github.com wrote:

Since you're running Mojave, I'll also include my older iMac (running High Sierra):

fname = getANTsRData("r16")

in_img = antsImageRead(fname)

full_image = in_img > -1

n4 = n4BiasFieldCorrection(in_img, mask = full_image)

sessioninfo::session_info( "ANTsRCore" )

─ Session info ───────────────────────────────────────────────────────────────

setting value

version R version 3.6.3 (2020-02-29)

os macOS High Sierra 10.13.6

system x86_64, darwin15.6.0

ui X11

language (EN)

collate en_US.UTF-8

ctype en_US.UTF-8

tz America/Los_Angeles

date 2020-03-19

─ Packages ───────────────────────────────────────────────────────────────────

package * version date lib source

ANTsRCore * 0.7.3.1 2020-03-19 [1] Github (ANTsX/ANTsRCore@5749f7c)

ITKR 0.5.3 2020-03-19 [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-19 [1] Github (RcppCore/Rcpp@5ea44f1)

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

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsRCore/pull/112#issuecomment-601464560, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLQFS22N2PGF4LVHD6TRIKUAZANCNFSM4KY3KLVA .

ntustison commented 4 years ago

Thanks @muschellij2 . Unfortunately, all three of my machines also work with in_img > 0.

muschellij2 commented 4 years ago

Still failing on mine, just reinstalled ANTsRCore. I don't really know what to do here, but it's making the n4 bias field correction unusable.

library(ANTsRCore) fname = getANTsRData("r16")

in_img = antsImageRead(fname)

full_image = in_img > -1

n4 = n4BiasFieldCorrection(in_img, mask = full_image)

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

THIS WORKS FINE

n4 = n4BiasFieldCorrection(in_img, mask = full_image)

SEGFAULTS

n4 = n4BiasFieldCorrection(in_img, mask = in_img > 0)

caught segfault

caught segfault address 0x10d006000, cause 'memory not mapped'

caught segfault address 0x10d012000, cause 'memory not mapped' address 0x10d00a000, cause 'memory not mapped'

Traceback:

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

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

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

I am on 3.6.2, so maybe that could be a reason? I highly doubt it:

sessioninfo::session_info("ANTsRCore") ─ 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-03-20

─ Packages ─────────────────────────────────────────────────────────────────── package * version date lib source ANTsRCore 0.7.3.1 2020-03-20 [1] Github (muschellij2/ANTsRCore@b6218e1) ITKR 0.5.3 2020-03-18 [1] Github (stnava/ITKR@715331d) 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) Rcpp 1.0.4.3 2020-03-20 [1] Github (RCppCore/Rcpp@5ea44f1) 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

On Thu, Mar 19, 2020 at 9:54 PM Nick Tustison notifications@github.com wrote:

Thanks @muschellij2 https://github.com/muschellij2 . Unfortunately, all three of my machines also work with in_img > 0.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsRCore/pull/112#issuecomment-601493832, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIGPLSDUWY3T6LEETTFZB3RILEGBANCNFSM4KY3KLVA .

ntustison commented 4 years ago

Can you set verbose = TRUE and copy-and-paste the output?

muschellij2 commented 4 years ago

It segfaults immediately:

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

Running N4 for 2-dimensional images.

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

Traceback:
 1: .helpn4BiasFieldCorrection(args)
 2: 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

 *** caught segfault ***

 *** caught segfault ***
address 0x10cc91000, cause 'memory not mapped'
address 0x10cc95000, cause 'memory not mapped'

So it seems like it's some weird ordering effect:

Can you try this full example:

library(ANTsRCore)
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()
rm(n4img); gc()
fname = getANTsRData("r16")
in_img = antsImageRead(fname)
n4 = n4BiasFieldCorrection(in_img)
rm(n4); gc()
mask = in_img > 0
mask2 = antsImageClone(mask, out_pixeltype = "float")
# fails
mask
sum(mask)
n4 = n4BiasFieldCorrection(in_img, mask = mask, verbose = TRUE)
# fails
n4 = n4BiasFieldCorrection(in_img, mask = mask2)

Weird stuff

This one works

I deleted the stuff before getANTsRData and it works fine!!

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)
rm(n4); gc()
#>           used (Mb) gc trigger  (Mb) limit (Mb) max used  (Mb)
#> Ncells 1849107 98.8    3536408 188.9         NA  2688371 143.6
#> Vcells 3302746 25.2    8388608  64.0      16384  5650762  43.2
mask = in_img > 0
mask2 = antsImageClone(mask, out_pixeltype = "float")
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)
n4 = n4BiasFieldCorrection(in_img, mask = mask2)

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

Session info ``` r sessioninfo::session_info() #> ─ 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-20 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> ANTsRCore * 0.7.3.1 2020-03-20 [1] Github (ANTsX/ANTsRCore@c53aaaa) #> 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.2 2020-02-28 [1] CRAN (R 3.6.0) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.0) #> digest 0.6.25 2020-02-23 [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.3 2020-03-18 [1] Github (stnava/ITKR@715331d) #> knitr 1.28 2020-02-06 [1] CRAN (R 3.6.0) #> 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) #> 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) #> 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) #> reprex 0.3.0.9001 2020-01-05 [1] Github (tidyverse/reprex@5ae0b29) #> rlang 0.4.5 2020-03-01 [1] CRAN (R 3.6.2) #> rmarkdown 2.1 2020-01-20 [1] CRAN (R 3.6.0) #> 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.6 2020-02-17 [1] CRAN (R 3.6.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.0) #> styler 1.3.2 2020-02-23 [1] CRAN (R 3.6.2) #> 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.12 2020-01-13 [1] CRAN (R 3.6.0) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 3.6.0) #> #> [1] /Library/Frameworks/R.framework/Versions/3.6/Resources/library ```
ntustison commented 4 years ago

So none of the normal screen dump gets printed to the screen?

muschellij2 commented 4 years ago

It's hard to demonstrate, but I made a quick video: https://youtu.be/dVcidFCeTZ0

ntustison commented 4 years ago

Awesome. Thanks for the video. I'm pretty sure I found it although it's not obvious. Let me push to the ANTs repository, I'll point out the issue, you can update the ANTs git tag, and see if it works for you.

ntustison commented 4 years ago

Here you go. I'm guessing this is the culprit.

muschellij2 commented 4 years ago

Installing now - will let you know if same behavior develops.