Writing your own fMRI programs using the AFNI API (Part 1)

I find that it’s fairly rare that I wish there was an AFNI program that did something that cannot be accomplished with existing tools and a bit of creativity.  But, if you do find something that requires writing a custom program, the AFNI distribution is both easily accessible and fairly straightforward to code for.  Over the next several months, I will cover how to program some very basic programs via the AFNI API (API = Application Programming Interface).  I primarily target Mac users here because 1) I’m writing this on a Mac; 2) writing instructions for every breed of Linux is exhausting.

To start, you will need to get the AFNI Source code (link).  Download and unpack the source into a directory that you will use for all of your AFNI development purposes.  Now it’s important to note that you don’t need to build the entire distribution in order to write your own programs.  Some of the harder to compile components of AFNI are responsible for drawing the GUI to the screen.  Skipping these makes our life easier, and usually AFNI programs run from the command line anyway.  With this in mind, the next step is to make a copy of the Makefile that best describes your Mac OS version (mine is Makefile.macosx_10.7_Intel_64) inside the source distribution.  Rename the copy simply “Makefile”.

If you have installed AFNI with Fink to satisfy the dependencies, you may already have what you need.  But as many of you know from previous posts, I suggest that you don’t use fink and instead use Homebrew.  First install Xcode and then via the Xcode Menu –> Preferences –> Downloads, install the Command Line Tools.  Now you can install homebrew, and then will need to install a few packages to aid in compiling.

brew install libpng jpeg expat freetype fontconfig

If you are using Homebrew, open the Makefile that you created and change any place where it says /sw to /usr/local.  Also, if you don’t want to install gcc via homebrew, change all of the references to GCC to be /usr/bin/gcc instead of the default /usr/local/bin/gcc.  Once these changes have been made, save and close your Makefile.

For both Homebrew and fink users.  Open a terminal window, and navigate to the AFNI sources folder.  To make your own AFNI programs, you mostly just need to build libmri.a, to do this just execute the following in your shell:

make libmri.a

Wait a while for the compiling to complete.  You should then see a file created called libmri.a.  This file (a library, or framework depending on your terminology) allows you to access any file type that the AFNI distribution supports (e.g. AFNI HEAD/BRIK and NIFTI and NIFTI Compressed).  At this point you can try to build a sample program to make sure everything is working.  The simpler programs can be found via

  cd afni_src
  wc 3d*.c | sort -n | head

In this case, I’ll use 3dEntropy as an example.  I’m not modifying the program at all for this, just changing the output name to 3dTest.  Notice the -I is telling GCC where to find all of the include headers and -L is telling the compiler where to find any linked libraries (e.g. libmri.a).  To test compile a program, try the following from inside of your AFNI source directory:

gcc 3dEntropy.c -o 3dTest -lmri -lXt -lf2c -lz -lexpat -I nifti/niftilib/ -I nifti/nifticdf -I nifti/znzlib -I /opt/X11/include/ -I rickr -L . -L /opt/X11/lib/

There you have it.  Test your program to see that it generates a help file and away you can go with writing your own AFNI programs.  I’d like to thank Daniel Glen and Rick Reynolds of the AFNI team for helping me get this setup working on my own computer.  I hope that by posting these instructions, others won’t have to ask as many questions as I did!

Happy coding!

Previous Post
Comments are closed.