Getting R Ready for Neuroimaging

There already exist some pretty good instructions on how to install ANTsR here. I still have had some people I work with ask me how to install it because sometimes it can be a bit quirky. So I’m briefly going over a way you can download it yourself. Note that ANTsR is currently only available for Mac and Linux.

ANTsR has the following dependencies.

mydeps <- c( "Rcpp", "tools", "methods", "magrittr" )
install.packages( pkgs = mydeps, dependencies = TRUE )

The packages listed here provide full functionality within ANTsR.

You will need CMake to download it but if you don’t already have it there’s a CMake package that can be downloaded specifically for R on github. The traditional way to install ANTsR is to clone the packages from github and use the R CMD INSTALL <packagename>. However, sometimes it’s easier to not have to go back and forth between terminal and R’s interface.

library(devtools)

install_github("stnava/cmaker")
install_github("stnava/ITKR")
install_github("stnava/ANTsR")

If you are having trouble updating ANTsR I’ve found that creating a clean build of ITKR and ANTsR (repeating installation) usually works.

Installing h5

The h5 package is a great package for saving compact data information and accessing without eating up all your RAM (which R does). It is on CRAN so install.packages('h5') should work. However, I had to download hd5c++ and ran into a little trouble. The recommendation is to use the following command in terminal.

brew install homebrew/science/hdf5 --enable-cxx

On the lab computer I was using I got an obnoxious error about using ‘brew install’ on files that I didn’t have permissions for. Unfortunately, the classic sudo remedy did not work so I had to go in and actually change file permissions. On Mac I went to the files and right clicked to open the “Open Info” option. I then unlocked the options in the bottom right corner and pressed th “+” button to add permissions for my profile. I also had to click on the settings icon beneath that and apply the changes to all files within. I had to do this withe the /usr/local/ folder and the /Library/ folder. I’m assuming there’s a similar file structure for linux. It’s probably easier to use the chmod command but I always forget the specific arguments for that command and was too lazy to read the help output.

While ANTsR does have functions that rely on h5, it is not widely used throughout the package. I would encourage you to try it out, especially if you’re working with large amounts of data. It has a slight learning curve and it took me nearly a full day working with it to figure out the important kinks before I could really use it comfortably. Once files are saved using h5 they can be accessed through R without actually loading them into active memory by using pointers isntead. This means that you need to be somewhat careful to not do anything that might overwrite the content you save. I like to use it for saving large matrices of images that I’ll be doing a lot of work with.

The syntax is pretty straightforward.

file <- h5file("h5test")
file["testvec"] <- 1:3
testvec <- file["testvec"]
testvec <- c(testvec, 4:6)
testvec[]

Comments

Popular posts from this blog

Align and Bias Correct Your Brain!

Making a Brain Template