Excluding NaNs from diffusion tensors

Hi all!

I used the commands dwi2tensor and subsequently tensor2metric in order to compute tensor components (the fa of the tensor and the mean diffusivity of the tensor) from dwi.mif files.

When I subsequently used the output from these operations for my next step of analysis (using SPM12 embedded in Matlab), I noticed that there are NaNs in the images (both in the fa and in the adc images) and this messes up the steps.

I am wondering whether there is an option to exclude NaNs right from the beginning because otherwise I have to tackle them at a later stage and it would seem very handy to be able to do that from the very start.

tensor2metric -fa - when specifying the option> adding a specification about excluding non-numerical values?

Maybe that is a bit of an unusual question, but I would be very curious to know if you can recommend a workaround this issue?

Thank you in advance for your advice on this.

Best wishes,

Hi Ivana,

To confirm, you are asking specifically about changing the NaN values present in the FA / MD images to some other finite value?

(I want to clarify as there’s been prior discussion about the exclusion of implausible values in the DWIs when performing the tensor fit; so this would be influencing whether or not the outcome of the tensor fit is reasonable, rather than trying to mitigate the effects of an unreasonable tensor fit after the fact)

If that is the case, then there’s nothing specifically within tensor2metric, but you can replace non-finite values with some other value after the fact:

mrcalc FA.mif -finite FA.mif 0.0 -if FA_nonan.mif

This places the value 0.0 in any voxel where the value of image FA.mif is not finite. For MD you however have the issue of determining an appropriate replacement value; it’s not a matter of “excluding non-numerical values” so much as substituting.


Hi Rob,

Thank you very much for your response.

I want to change NaN values present in the FA/MD images to a finite value (I guess), just to be different than the value 0. I will try with the mrcalc solution you suggested and get back to the discussion in case this is useful for someone else too.

As for the MD, would you suggest trying to substitute non-numerical values with a command such as mrdegibbs.

Thank you once again and have a great day,

Hi Rob,

I tried running the mrcalc solution you suggested, using the nii. files I have got (not mif.) but I get an error code: Could not interpret string “fa_nii.” (file I am using) as either an image path or a numerical value.

I tried changing the name of the respective file and calling the command from the folder where the file is located in (in order not to specify the whole directory’s name, but only the file name) but the command still did not get accepted.

Any thoughts on why this could be the case?

Thank you and regards,

If the filename you passed was genuinely fa_nii., then that’s indeed not an image that we’d recognise. It needs to have the suffix .nii (or .nii.gz) for it to be recognised as a NIfTI image.

If that’s not it, try running any other command on that file in debug mode, e.g. mrinfo -debug fa.nii, and see whether that provides any more information.


Thank you for this advice. The file is called ‘fa_sub07ses1.nii’ and I tried renaming it to remove the underscore or numbers and I still get the error. When I tried to run it in debug mode, it came up with the following:

mrinfo: [INFO] reading config file “/etc/mrtrix.conf”…
mrinfo: [DEBUG] reading key/value file “/etc/mrtrix.conf”…
mrinfo: [DEBUG] No config file found at “/home/ivana/.mrtrix.conf”
mrinfo: [INFO] opening image “fa_sub07ses1.nii”…
mrinfo: [DEBUG] memory-mapping file “fa_sub07ses1.nii”…
mrinfo: [DEBUG] file “fa_sub07ses1.nii” mapped at 0x7fdbe8201000, size 4587872 (read-only)
mrinfo: [DEBUG] transforms_match: FOV difference in scanner coordinates: 0.024943885309450468
mrinfo: [DEBUG] unmapping file “fa_sub07ses1.nii”
mrinfo: [DEBUG] sanitising image information…
mrinfo: [INFO] Axes and transform of image “fa_sub07ses1.nii” altered to approximate RAS coordinate system
mrinfo: [DEBUG] searching for suitable phase encoding data…
mrinfo: [INFO] image “fa_sub07ses1.nii” opened with dimensions 128x128x70, voxel spacing 1.75x1.75x2, datatype Float32LE

Image: “fa_sub07ses1.nii”

Dimensions: 128 x 128 x 70
Voxel size: 1.75 x 1.75 x 2
Data strides: [ -1 2 3 ]
Format: NIfTI-1.1
Data type: 32 bit float (little endian)
Intensity scaling: offset = 0, multiplier = 1
Transform: 0.9992 -0.03874 1.455e-11 -105.6
0.03874 0.9992 -0.004538 -113.9
0.0001758 0.004534 1 -107.2
comments: 5.0.10
mrtrix_version: 3.0_RC3-51-g52a2540d

Any further advice on this would be greatly appreciated.


Ok, so the NIfTI file is fine after the rename since mrinfo can read it without error. Can you copy/paste the exact command line that you used for the mrcalc call?


Actually the file name was the above one from the very beginning (the fa_nii thing was just to try and shorten providing my full path directories for the sake of brevity :innocent: > but not a good exemplar name indeed!)

So this is the exact command line:

mrcalc /Fridge/users/ivana/regFAs_ses1_ses2/sub07/fa_sub07ses1.nii -finite /Fridge/users/ivana/regFAs_ses1_ses2/sub07/fa_sub07ses1.nii -if /Fridge/users/ivana/regFAs_ses1_ses2/sub07/fa_sub07ses1_nonan.nii

Maybe I am missing something very obvious (still quite a novice with that!)

Thanks once again for your advice!

Ah, ok. There’s a missing zero before the -if


Thank you very much for pointing this out! It deem seem to run now!

Have a nice day,