# Excluding NaNs from diffusion tensors

**URL:** https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835
**Category:** Uncategorized
**Created:** [June 26, 2020, 4:02pm UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835 "2020-06-26T16:02:36Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Ivana](https://community.mrtrix.org/user_avatar/community.mrtrix.org/ivana/32/2469_2.png) [@Ivana](https://community.mrtrix.org/u/Ivana)
#### Post date: [June 26, 2020, 4:02pm UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/1 "2020-06-26T16:02:36Z")

</div>

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,  
Ivana

---

<div class="post-metadata">

### Author: ![rsmith](https://community.mrtrix.org/user_avatar/community.mrtrix.org/rsmith/32/2672_2.png) [@rsmith](https://community.mrtrix.org/u/rsmith)
#### Post date: [June 28, 2020, 3:21am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/2 "2020-06-28T03:21:11Z")

</div>

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](https://community.mrtrix.org/t/dwi2tensor-wrong-values/1860) 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_.

Rob

---

<div class="post-metadata">

### Author: ![Ivana](https://community.mrtrix.org/user_avatar/community.mrtrix.org/ivana/32/2469_2.png) [@Ivana](https://community.mrtrix.org/u/Ivana)
#### Post date: [June 28, 2020, 10:02am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/3 "2020-06-28T10:02:50Z")

</div>

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,  
Ivana

---

<div class="post-metadata">

### Author: ![Ivana](https://community.mrtrix.org/user_avatar/community.mrtrix.org/ivana/32/2469_2.png) [@Ivana](https://community.mrtrix.org/u/Ivana)
#### Post date: [June 28, 2020, 7:01pm UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/4 "2020-06-28T19:01:59Z")

</div>

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,  
Ivana

---

<div class="post-metadata">

### Author: ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)
#### Post date: [June 28, 2020, 7:09pm UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/5 "2020-06-28T19:09:13Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Ivana](https://community.mrtrix.org/user_avatar/community.mrtrix.org/ivana/32/2469_2.png) [@Ivana](https://community.mrtrix.org/u/Ivana)
#### Post date: [June 29, 2020, 7:23am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/6 "2020-06-29T07:23:26Z")

</div>

Hello,

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.

Ivana

---

<div class="post-metadata">

### Author: ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)
#### Post date: [June 29, 2020, 7:36am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/7 "2020-06-29T07:36:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![Ivana](https://community.mrtrix.org/user_avatar/community.mrtrix.org/ivana/32/2469_2.png) [@Ivana](https://community.mrtrix.org/u/Ivana)
#### Post date: [June 29, 2020, 7:52am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/8 "2020-06-29T07:52:26Z")

</div>

> [@Ivana](#):
>
> mrdegibbs

Hi,

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 😇 \> but not a good exemplar name indeed!)

So this is the exact command line:

```auto
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!

---

<div class="post-metadata">

### Author: ![jdtournier](https://community.mrtrix.org/user_avatar/community.mrtrix.org/jdtournier/32/2594_2.png) [@jdtournier](https://community.mrtrix.org/u/jdtournier)
#### Post date: [June 29, 2020, 8:26am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/9 "2020-06-29T08:26:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ivana](https://community.mrtrix.org/user_avatar/community.mrtrix.org/ivana/32/2469_2.png) [@Ivana](https://community.mrtrix.org/u/Ivana)
#### Post date: [June 29, 2020, 9:01am UTC](https://community.mrtrix.org/t/excluding-nans-from-diffusion-tensors/3835/10 "2020-06-29T09:01:53Z")

</div>

Hi,

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

Have a nice day,  
Ivana
