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

How to pass command line arguments to antsRegistration? #391

Open muratmaga opened 8 months ago

muratmaga commented 8 months ago

If I set the printArgs to TRUE, I can see the full set of antsRegistration parameters.

 mytx2 <- antsRegistration(fixed=fi,  moving=mi, typeofTransform = 'SyN', printArgs = TRUE)
antsRegistration -d 2 -r [0x56123776d170,0x56123c7e44e0,1] -m mattes[0x56123776d170,0x56123c7e44e0,1,32,regular,0.2] -t Affine[0.25] -c 2100x1200x1200x0 -s 3x2x1x0 -f 4x2x2x1 -x [NA,NA] -m mattes[0x56123776d170,0x56123c7e44e0,1,32] -t SyN[0.2,3,0] -c [ 40x20x0 ,1e-7,8] -s 2x1x0 -f 4x2x1 -u 0 -z 1 -o [/tmp/RtmpP0uvUG/file33d6b650120ba,0x56123652cab0,0x56123c317c70] -x [NA,NA] --float 1 --random-seed 1 --write-composite-transform 0 

I want to use this to modify some of the settings that are not exposed by R call. However, I couldn't figure out how to pass this back to antsRegistration. Documentation shows an example, but the call fails since it points to a file on disk, as opposed to an array in memory.


 antsRegistration( list( d=2,m="mi[r16slice.nii.gz,r64slice.nii.gz,1,20,Regular,0.05]", t="affine[1.0]", c="2100x1200x1200x0",  s="3x2x1x0", f="4x3x2x1", u="1", o="[xtest,xtest.nii.gz,xtest_inv.nii.gz]" ) )```
ncullen93 commented 7 months ago

You'll have to look through the antsRegistration code in ANTsR and ANTsRCore probably more specifically, because I don't believe there is a way to add any additional parameters. Happy to accept a PR if you think something is missing... it should be easy to just add the function argument to antsRegistration and then add that arg to the parameters that are built up.

cookpa commented 7 months ago

I've not used the list option but I believe you would need to replace file names with pointers to antsImage objects using antsrGetPointerName, eg

img_pt <- antsrGetPointerName( antsImageClone( img ))

I include the clone here from the man example, the antsRegistration function also clones the input image.

muratmaga commented 7 months ago

@cookpa Thanks for the pointer info. However, if I do antsImageClone, the pointer address changes every time. Is there reason antsr is not directly using the actual address of the image?

> fi <- antsImageRead(getANTsRData("r16") )
> mi <- antsImageRead(getANTsRData("r64") )
> 
> antsrGetPointerName(fi)
[1] "0x561b9ba33b30"
> antsrGetPointerName(mi)
[1] "0x561b9bd504e0"
> 
> 
> mytx2 <- antsRegistration(fi,  mi, typeofTransform = 'Affine', printArgs = T)
antsRegistration -d 2 -r [0x561b9c78e900,0x561b9c7cec50,1] -m mattes[0x561b9c78e900,0x561b9c7cec50,1,32,regular,0.2] -t Affine[0.25] -c 2100x1200x1200x10 -s 3x2x1x0 -f 6x4x2x1 -u 0 -z 1 -o [/tmp/Rtmp01coMb/file118bd916264147,0x561b9bd30860,0x561b98aece90] -x [NA,NA] --float 1 --random-seed 1 --write-composite-transform 0 
> antsrGetPointerName(antsImageClone(fi))
[1] "0x561b9bc6f0c0"
> antsrGetPointerName(antsImageClone(fi))
[1] "0x561b9bed7170"

clearly antsRegistration is using the antsImageClone too, since every call has a different pointer address. So not sure how to do convert this a list call

> mytx2 <- antsRegistration(fi,  mi, typeofTransform = 'Affine', printArgs = T)
antsRegistration -d 2 -r [0x561b9ba32fd0,0x561b9bac0570,1] -m mattes[0x561b9ba32fd0,0x561b9bac0570,1,32,regular,0.2] -t Affine[0.25] -c 2100x1200x1200x10 -s 3x2x1x0 -f 6x4x2x1 -u 0 -z 1 -o [/tmp/Rtmp01coMb/file118bd91a9f7941,0x561b9f72e800,0x561b9bc68ab0] -x [NA,NA] --float 1 --random-seed 1 --write-composite-transform 0 
> mytx2 <- antsRegistration(fi,  mi, typeofTransform = 'Affine', printArgs = T)
antsRegistration -d 2 -r [0x561b9bc68fc0,0x561b9cbdb9c0,1] -m mattes[0x561b9bc68fc0,0x561b9cbdb9c0,1,32,regular,0.2] -t Affine[0.25] -c 2100x1200x1200x10 -s 3x2x1x0 -f 6x4x2x1 -u 0 -z 1 -o [/tmp/Rtmp01coMb/file118bd96a38f2d9,0x561b9c6a47c0,0x561b9bd303f0] -x [NA,NA] --float 1 --random-seed 1 --write-composite-transform 0 
cookpa commented 7 months ago

Yes you'd get another pointer each time, they are pointers to copies of the image. Sorry, the example I gave was probably not very useful. To avoid garbage collection you'd need to keep a reference around, so something like

fi_for_reg = antsImageClone(fi) mi_for_reg = antsImageClone(mi)

Then in your antsRegistration parameter list, you'd replace the file names with antsrGetPointerName(fi_for_reg) and antsrGetPointerName(mi_for_reg)

I don't actually know why antsRegistration clones the input images this way, but it does.