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

How to save output of antsrMotionCalculation as one file #272

Open
ptsii opened this issue Jun 10, 2019 · 1 comment
Open

How to save output of antsrMotionCalculation as one file #272

ptsii opened this issue Jun 10, 2019 · 1 comment

Comments

@ptsii
Copy link

ptsii commented Jun 10, 2019

My apologies if this is posted in the wrong place... I'm running antsrMotionCalculation on a large file and want to save the output to disk for use separately, later, to be called by a different script. Right now I save everything in different files, e.g.,:

bold4D_motionCalc$moco_img
bold4D_motionCalc$moco_params
bold4D_motionCalc$fd
bold4D_motionCalc$dvars
bold4D_motionCalc$moco_mask
bold4D_motionCalc$moco_avg_img

...but is there a way to save the whole complex bold4D_motionCalc object as one file?

-Tom

@muschellij2
Copy link
Collaborator

There are a few options. You can save the whole object as the list it already is using save or saveRDS. The biggest issue with this is that the antsImage objects will have external pointers, which will be invalid the next time you open R. You can convert these to nifti objects, or something without an external pointer. Here I use the extrantsr package: https://github.com/muschellij2/extrantsr

library(extrantsr)
bold4D_motionCalc = lapply(bold4D_motionCalc, function(x) {
  if (is.antsImage(x)) {
    x = ants2oro(x)
  } 
  x
})

You may want to change this when you read it back in. See below.

Working Example

library(ANTsRCore)
#> 
#> Attaching package: 'ANTsRCore'
#> The following object is masked from 'package:stats':
#> 
#>     var
#> The following objects are masked from 'package:base':
#> 
#>     all, any, apply, max, min, prod, range, sum
library(ANTsR)
library(extrantsr)
#> 
#> Attaching package: 'extrantsr'
#> The following objects are masked from 'package:ANTsRCore':
#> 
#>     origin, origin<-
Sys.setenv(ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS = 1)
Sys.setenv(ANTS_RANDOM_SEED = 1)
set.seed(120)
simimg<-makeImage(rep(5,4), rnorm(5^4))
testthat::expect_equal(mean(simimg), 0.0427369860965759)
bold4D_motionCalc = antsrMotionCalculation( simimg , seed = 1234)

bold4D_motionCalc = lapply(bold4D_motionCalc, function(x) {
  if (is.antsImage(x)) {
    x = ants2oro(x)
  } 
  x
})
tfile = tempfile(fileext = ".rds")
saveRDS(bold4D_motionCalc, file = tfile)
rm(bold4D_motionCalc); 
for (i in 10) gc();


bold4D_motionCalc = readRDS(file = tfile)
bold4D_motionCalc = lapply(bold4D_motionCalc, function(x) {
  if (oro.nifti::is.nifti(x)) {
    x = oro2ants(x)
  } 
  x
})

bold4D_motionCalc
#> $moco_img
#> antsImage
#>   Pixel Type          : float 
#>   Components Per Pixel: 1 
#>   Dimensions          : 5x5x5x5 
#>   Voxel Spacing       : 1x1x1x1 
#>   Origin              : 0 0 0 0 
#>   Direction           : 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 
#>   Filename           : /private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/Rtmps21mcq/file130527f735643.nii.gz 
#> 
#> 
#> $moco_params
#>      MOCOparam1 MOCOparam2 MOCOparam3 MOCOparam4 MOCOparam5 MOCOparam6
#> [1,]  0.8070312 -2.7196524  -7.722021          0          0          0
#> [2,]  0.6403608  0.7210882   0.331462          0          0          0
#> [3,] -3.5773988  0.7025864  -1.792493          0          0          0
#> [4,]  3.4404235 -2.8812182   2.718754          0          0          0
#> [5,]  2.5139089  0.2271850   2.987108          0          0          0
#> 
#> $moco_avg_img
#> antsImage
#>   Pixel Type          : float 
#>   Components Per Pixel: 1 
#>   Dimensions          : 5x5x5 
#>   Voxel Spacing       : 1x1x1 
#>   Origin              : 0 0 0 
#>   Direction           : 1 0 0 0 1 0 0 0 1 
#>   Filename           : /private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/Rtmps21mcq/file130526d248a13.nii.gz 
#> 
#> 
#> $moco_mask
#> antsImage
#>   Pixel Type          : float 
#>   Components Per Pixel: 1 
#>   Dimensions          : 5x5x5 
#>   Voxel Spacing       : 1x1x1 
#>   Origin              : 0 0 0 
#>   Direction           : 1 0 0 0 1 0 0 0 1 
#>   Filename           : /private/var/folders/1s/wrtqcpxn685_zk570bnx9_rr0000gr/T/Rtmps21mcq/file1305279e51d5f.nii.gz 
#> 
#> 
#> $fd
#>   MeanDisplacement MaxDisplacement
#> 1         8.759284        8.759284
#> 2         4.722396        4.722396
#> 3         9.079914        9.079914
#> 4         3.254630        3.254630
#> 5         0.000000        0.000000
#> 
#> $dvars
#> [1] 1.271681 1.421977 1.572031 1.651933 1.712464

Created on 2019-06-10 by the reprex package (v0.2.1)

Hope that helps.

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

2 participants