Reading peak values in python

Hi all,
My question relates to analyzing the ODF peaks extracted from the FOD using sh2peaks.
I would like to get the “main” direction for each voxel, or more specifically get a mask of the voxels where the “main” direction is left to right.
from reading here, I got the feeling the easiest way for me to to do this would be to convert the peak file to a nifti file and use python for the rest, but while using nibabel.load gets me all the correct metadata and data shape, the data itself consists of nans.
Is there a reason for that, or a simpler way to do what I am trying to do?

To be clear, I’m using dti2fod, sh2peaks and then mrconvert to pick only the first peak and convert to nifti.

Thanks!

Just a couple of points:

  • you can use the sh2peaks -num to restrict its output to the first peak only – no need for the additional mrconvert call.

  • what does mrstats say about your NIfTI file? If all the measures come out as NaN, then there is indeed a problem. But I expect what you’re seeing is all the NaNs in the background of the image (outside your mask). In other words, there is data in there, but also a lot of NaNs in non-brain regions. You can also verify this by loading your NIfTI image in mrview (NaNs show up with value: ?).

  • one potential source of error might be that you’re extracting the wrong set of volumes, by using mrconvert -coord 3 1:3 (extracts volumes 2 to 4), rather than mrconvert -coord 3 0:2 (extracts the first 3 volumes).

I ran into this problem too,

Bit of an aside but helpful for those who also stumble onto this:

As a lot of the datapoints in the array are coded as NaNs when loaded via nibabel in python, You might mistake the entire array to be NaNs as np.max and np.min both yield NaN.

Instead you should use np.nanmax and np.nanmin.
These will print the correct statistics excluding the NaNs, and produce a RuntimeWarning if the array only contains NaNs.

In my case this cleared up the issue.

2 Likes