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

ITK bug in createJacobianDeterminantImage after upgrade #383

Closed muratmaga closed 1 year ago

muratmaga commented 1 year ago

A couple weeks ago I upgraded the ANTsR and ANTsCore to the latest version on GH via R CMD INSTALL method. Now I am getting this ITK error when I try to run createJacobianDeterminantImage at the warp image read step. This reproduces using the example code.

antsImageRead() works perfectly fine on this image, so it is not triggered due to file corruption

> fi<-antsImageRead( getANTsRData("r16") ,2)
> mi<-antsImageRead( getANTsRData("r64") ,2)
> fi<-resampleImage(fi,c(128,128),1,0)
> mi<-resampleImage(mi,c(128,128),1,0)
> mytx<-antsRegistration(fixed=fi , moving=mi, typeofTransform = c("SyN") )
> jac<-createJacobianDeterminantImage(fi,mytx$fwdtransforms[[1]],1)
ITK ExceptionObject caught!
Error in createJacobianDeterminantImage(fi, mytx$fwdtransforms[[1]], 1) : 
  /usr/local/lib/R/site-library/ITKR/libs/include/ITK-5.3/itkImageFileReader.hxx:132:
 Could not create IO object for reading file /tmp/RtmpB9ydC3/file20a543a7faac81Warp.nii.gz
  Tried to create one of the following:
    MGHImageIO
  You probably failed to set a file suffix, or
    set the suffix to an unsupported type.

Here are the sessionInfo and other relevant things.

  Dependency                                   GitTag
1       ANTs d30526f9cb5159bc0a3e9011f7ae5f409b3634c8
2  ANTsRCore 038b62f258d426e82ec3dd837793a7746b5d83b8
3    ANTsURL        https://github.com/ANTsX/ANTs.git
4        ITK                                 v5.3rc04
> .libPaths()
[1] "/home/maga/R/x86_64-pc-linux-gnu-library/4.1" "/usr/local/lib/R/site-library"               
[3] "/usr/lib/R/site-library"                      "/usr/lib/R/library"                          
> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.5 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8    
 [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8    LC_PAPER=en_CA.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ANTsR_0.5.7.5   ANTsRCore_0.7.5

loaded via a namespace (and not attached):
[1] compiler_4.1.2      RcppEigen_0.3.3.9.3 magrittr_2.0.3      Matrix_1.4-0        tools_4.1.2         ITKR_0.6.0.0.2     
[7] Rcpp_1.0.9          grid_4.1.2          lattice_0.20-45    
> 
> 
ntustison commented 1 year ago

I'm not able to reproduce. And that ITK error looks wrong as it's missing several file types. You mention that you ugraded ANTsRCore and ANTsR but I don't see where you completely re-installed ITKR. You might want to try that for a possible fix.

muratmaga commented 1 year ago

Sorry, yes ITKR also got reinstalled. And I would agree this is an ITK issue, except that antsImageRead seem to work fine with the same input. As a temporary work around I brought back the older libraries, and everything is back to working.

I will try to rebuild the ITKR cleanly one more and see if it fixes.

ntustison commented 1 year ago

antsImageRead seem to work fine with the same input.

"input", I assume, refers to the temporary warp file, mytx$fwdtransforms[[1]], correct?

muratmaga commented 1 year ago

it I used the hard coded path reported in the error, because I suspected the file got corrupted for some reason. antsImageRead("/tmp/RtmpB9ydC3/file20a543a7faac81Warp.nii.gz")

ntustison commented 1 year ago

Okay, just wanted to confirm as "input" was ambiguous and the antsImageRead call of the temp warp file is missing. I just tried it and wasn't able to reproduce the error:

> mytx$fwdtransforms[[1]]
[1] "/var/folders/5h/62f7bql14zvgx34nqwd7xvnc0000gn/T//RtmpaA01Qa/file2f11583079921Warp.nii.gz"
> antsImageRead( mytx$fwdtransforms[[1]] )
antsImage
  Pixel Type          : float 
  Components Per Pixel: 2 
  Dimensions          : 128x128 
  Voxel Spacing       : 2x2 
  Origin              : 0 0 
  Direction           : 1 0 0 1 
  Filename           : /var/folders/5h/62f7bql14zvgx34nqwd7xvnc0000gn/T//RtmpaA01Qa/file2f11583079921Warp.nii.gz 
muratmaga commented 1 year ago

This is what I am getting

>  fi<-antsImageRead( getANTsRData("r16") ,2)
>      mi<-antsImageRead( getANTsRData("r64") ,2)
>      fi<-resampleImage(fi,c(128,128),1,0)
>      mi<-resampleImage(mi,c(128,128),1,0)
>      mytx<-antsRegistration(fixed=fi , moving=mi, typeofTransform = c("SyN") )
>      jac<-createJacobianDeterminantImage(fi,mytx$fwdtransforms[[1]],1)
ITK ExceptionObject caught!
Error in createJacobianDeterminantImage(fi, mytx$fwdtransforms[[1]], 1) : 
  /usr/local/lib/R/site-library/ITKR/libs/include/ITK-5.3/itkImageFileReader.hxx:132:
 Could not create IO object for reading file /tmp/RtmpUA2gDC/file211d1753c51c041Warp.nii.gz
  Tried to create one of the following:
    MGHImageIO
  You probably failed to set a file suffix, or
    set the suffix to an unsupported type.
> test = antsImageRead(mytx$fwdtransforms[[1]])
> print(test)
antsImage
  Pixel Type          : float 
  Components Per Pixel: 2 
  Dimensions          : 128x128 
  Voxel Spacing       : 2x2 
  Origin              : 0 0 
  Direction           : 1 0 0 1 
  Filename           : /tmp/RtmpUA2gDC/file211d1753c51c041Warp.nii.gz 

Will try to rebuild ITKR from scratch

muratmaga commented 1 year ago

Himm, this is what I did

root@magalab-ML:~# cd ITKR
root@magalab-ML:~/ITKR# ./cleanup
root@magalab-ML:~/ITKR# git pull origin
Already up to date.
root@magalab-ML:~/ITKR# cd ..
root@magalab-ML:~# R CMD INSTALL ITKR

successful build, but resulted in the same error. any other suggestions

ntustison commented 1 year ago

Is the error reproducible on a different machine?

muratmaga commented 1 year ago

Yep, it does on a cloud Ubuntu machine I have. Same error. I can give you access, if it helps: image

muratmaga commented 1 year ago

I am still getting the same error after building ITKR, ANTsCore, ANTsR via R CMD INSTALL with fresh clones from the primary repo on a new system. Any help will be greatly appreciated...

ntustison commented 1 year ago

I don't know. I'd have to explore a bit. If you can provide access, I can try to take a look later this week.

muratmaga commented 1 year ago

just sent you an email with access information.

ntustison commented 1 year ago

Okay, I don't know why this is happening on your systems but the CMake magic that is supposed to register the IO factories is just not working when compiling ANTsR but not with ANTsRCore. That's why createJacobianDeterminantImage doesn't work but antsImageRead does. I wish I figured out a fix it but the issue appears to be system-specific and I don't have more time to play around with this at the moment. In fact, if I had more time, I would avoid having it read the file and simply input the actual ANTs image. Perhaps you should add that as a wish-list item.

For now, a workaround is to manually add the required IO factories prior to calling R CMD INSTALL ANTsR. For example, to register the NiftiIO, you would add the line

itk::NiftiImageIOFactory::RegisterOneFactory();

before line 40 of CreateJacobianDeterminantImage. You would also need to include the corresponding header #include "itkNiftiImageIOFactory.h" at the top. Fortunately, it looks like this is the only file affected.

muratmaga commented 1 year ago

What a bizarre situation. Thank you very much for finding a workaround. Much appreciated