# Spherical Harmonic Coeffs to DWI images

**URL:** <https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014>\
**Category:** Uncategorized\
**Created:** [October 30, 2018, 5:48pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014 "2018-10-30T17:48:35Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![ADavis](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ADavis](https://community.mrtrix.org/u/ADavis)\
**Post date:** [October 30, 2018, 5:48pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/1 "2018-10-30T17:48:35Z")

</div>

Hello all in the MRtrix Community,

I’m trying to replicate the method of [Mirzaalian et al., 2016](https://doi.org/10.1016/j.neuroimage.2016.04.041), in which spherical harmonic deconvolution is used to harmonize DMRI data from different scanners (with similar acquisition schemes). Basically they calculated scaling factors on the SH coefficient images using RISH values from their test subjects at each site. I can see that the `dwi2fod` command in MRtrix gets me part of the way there to implementing this – I now have coefficient images. But is there a command to go in the opposite direction? That is, to recreate the DWI data from the coefficients after I modify them? The idea would be that, using this method, any DMRI processing software should be able to handle the harmonized images.

Thanks,  
Andrew

---

<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:** [October 31, 2018, 8:05pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/2 "2018-10-31T20:05:34Z")

</div>

I have a feeling you’re after the [amp2sh](https://mrtrix.readthedocs.io/en/latest/reference/commands/amp2sh.html) and [sh2amp](https://mrtrix.readthedocs.io/en/latest/reference/commands/sh2amp.html) commands for this. [dwi2fod](https://mrtrix.readthedocs.io/en/latest/reference/commands/dwi2fod.html) does a fair bit more than a straight SH fit, which I think is what you’re after here?

---

<div class="post-metadata">

**Author:** ![ADavis](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@ADavis](https://community.mrtrix.org/u/ADavis)\
**Post date:** [November 1, 2018, 1:16am UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/3 "2018-11-01T01:16:41Z")

</div>

Those look like they will do the trick. Thank you!

---

<div class="post-metadata">

**Author:** ![stefan](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://community.mrtrix.org/u/stefan)\
**Post date:** [January 31, 2019, 5:29pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/4 "2019-01-31T17:29:02Z")

</div>

Dear J-Donald,

I have two questions regarding amp2sh and sh2amp to project back and forth between DWI and SH:

1. When I apply amp2sh and try to reconstruct then DWI from the resulting SH, it seems to me I do not get the original DWI volumes. Is this discrepancy expected, or am I doing something wrong? I do not normalise to b0 volumes, and I use the max number of l (i.e. for that data 6)

2. When applying sh2amp, I do either get no b0 volumes at all (that is the case when I feed a gradient file with [x,y,z,b] to sh2amp), or the b0 volumes are completely empty (when I feed a file with [x,y,z], I assume a zero vector just means no signal). How can I recover b0 volumes from SH, or is that not possible at all?

Many thanks for your help in advance  
Stefan

---

<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:** [January 31, 2019, 5:55pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/5 "2019-01-31T17:55:37Z")

</div>

1. The amplitude to SH transform generally entails rank reduction: the number of SH coefficients will typically be fewer than the number of DW signals you’re fitting to – unless the number of DW volumes matches exactly (i.e. 28 volumes for _l_max=6). This means that there’s always some degree of ‘smoothing’ or information loss involved, and you won’t in general be able to reproduce the input signals exactly (unless, as I mentioned above, the number of SH coefficients matches the number of DW signals exactly).

2. The SH fit is done on a per-shell basis. So by default, the fit is done to the highest shell available, all other shells are ignored. To get around this would require extending the SH basis to also apply along the radial / _b_-value domain – which is what we tried to do with [this recent paper](https://arxiv.org/abs/1806.06456). Watch this space…

---

<div class="post-metadata">

**Author:** ![stefan](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://community.mrtrix.org/u/stefan)\
**Post date:** [January 31, 2019, 6:17pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/6 "2019-01-31T18:17:16Z")

</div>

Great thanks for you quick reply.

1. Okay, that is what I expected, but I am happy you kindly confirmed.
2. You are referring to DWI -\> SH on multi shell data if I am not mistaken, but I was wondering if I had single shell data, applying amp2sh I get my SH coeff. Then I want to retrieve my DWI signal from SH again (sh2amp), but then the b0 volumes cannot be reconstructed from SH coeff? Or am I misunderstanding…

---

<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:** [January 31, 2019, 7:40pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/7 "2019-01-31T19:40:55Z")

</div>

Regarding point 2: the fit is done per shell, and a shell means: the set of volumes acquired with the same b-value (and different directions). So by that definition, the b=0 volumes constitute a separate shell – albeit a very small one – so they won’t be part of the outer shell’s SH fit, and can’t be regenerated from its coefficients.

If you think about it, that also means that a single-shell HARDI acquisition is already actually multi-shell… 🤯 Which is why we can do 2-tissue multi-shell multi-tissue (MSMT) CSD with regular single-shell acquisitions. 🤓

---

<div class="post-metadata">

**Author:** ![stefan](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://community.mrtrix.org/u/stefan)\
**Post date:** [February 1, 2019, 11:05am UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/8 "2019-02-01T11:05:34Z")

</div>

Awesome. Thanks for the clarification, very insightful. I also will check out the paper you’ve pointed me to.

---

<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:** [February 10, 2019, 2:07pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/9 "2019-02-10T14:07:43Z")

</div>

> How can I recover b0 volumes from SH, or is that not possible at all?

To answer this question in the most literal sense, independently of the capabilities and interfaces of the relevant _MRtrix3_ commands:

1. Extract the _b_=0 volumes from your DWI dataset.

2. Compute the mean image intensity across volumes.

3. Congratulations! You just did the equivalent of performing a _lmax_=0 least-squares SH fit to your _b_=0 “shell” (which is the maximum spherical harmonic degree achievable for such data), and mapped the result back to the predicted “DWI” (_b_=0) image intensities.

---

<div class="post-metadata">

**Author:** ![stefan](https://community.mrtrix.org/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@stefan](https://community.mrtrix.org/u/stefan)\
**Post date:** [June 14, 2019, 7:13pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/10 "2019-06-14T19:13:18Z")

</div>

A further question regarding sh2amp. When i use am2sh to construct SH and then reverse the operation with sh2amp, it seem that the diffusion sensitisation is flipped for the x-axis. I use a gradient file for the directions ([x,y,z]), when I adjust the directions to [-x,y,z] it seems I get nearly the same signal as in the original DWI volume. Am I doing something wrong, or is this maybe an inconsistency with FSL and MRtrix?

Thanks in advance  
Stefan

---

<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 16, 2019, 1:24pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/11 "2019-06-16T13:24:36Z")

</div>

> When i use amp2sh to construct SH and then reverse the operation with sh2amp, it seem that the diffusion sensitisation is flipped for the x-axis. I use a gradient file for the directions ([x,y,z]), when I adjust the directions to [-x,y,z] it seems I get nearly the same signal as in the original DWI volume. Am I doing something wrong, or is this maybe an inconsistency with FSL and MRtrix?

To know for sure, we would need to know _exactly_:

- Where the diffusion gradients are obtained for the initial `amp2sh` call;

- How you derive the external gradient file that you use for the `sh2amp` call;

- Which version of _MRtrix3_ you are using (particularly whether it is newer than [this fix](http://community.mrtrix.org/t/bug-in-fsl-bvecs-handling/165)).

---

<div class="post-metadata">

**Author:** ![mlyon](https://community.mrtrix.org/user_avatar/community.mrtrix.org/mlyon/32/2046_2.png) [@mlyon](https://community.mrtrix.org/u/mlyon)\
**Post date:** [October 6, 2021, 2:42pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/12 "2021-10-06T14:42:33Z")

</div>

**Adding an update here as I am experiencing the exact same issue.**

> - Where the diffusion gradients are obtained for the initial `amp2sh` call

I’m using the HCP dataset and the following commandline

```auto
amp2sh -shells 1000 -fslgrad bvecs bvals data.nii.gz sh_coeffs.nii.gz

```

> - How you derive the external gradient file that you use for the `sh2amp` call

This is derived simply by taking the `b = 1000` b-vectors from the `bvecs` file and transposing them so that the resulting `b1000_bvecs` file will have 3 columns and 90 rows.  
then using the command

```auto
sh2amp sh_coeffs.nii.gz b1000_bvecs out.nii.gz

```

Exactly the same as @stefan, the x axis attenuation seems flipped, and simply by modifying the `b1000_bvecs` from `[x, y, z]` to `[-x, y, z]`, the resultant image from `sh2amp` is almost exactly perfect.

> - Which version of _MRtrix3_ you are using

I’m using version 3.0.2

---

<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:** [October 6, 2021, 2:58pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/13 "2021-10-06T14:58:27Z")

</div>

OK, the issue here will relate to the conventions assumed in how the DW directions are stored in MRtrix and FSL formats. The `sh2amp` call expects the directions to be provided in MRtrix format, which does not make the same assumptions as the FSL bvecs/bvals format. You’ll find [all the details in our docs](https://mrtrix.readthedocs.io/en/latest/concepts/dw_scheme.html), but the main point is that directions in MRtrix format are assumed to be provided with respect to the scanner/word coordinates, while FSL assumes them to be provided with respect to the image frame – with an additional (and unexpected!) potential flip of the _x_-axis depending on the handedness of the image coordinate system (i.e. whether the image is stored in ‘neurological’ or ‘radiological’ convention, or more precisely, the sign of the determinant of the transform matrix). That last gotcha has [caught us out in the past](https://www.mrtrix.org/2016/04/15/bug-in-fsl-bvecs-handling/)…

If you’re using MRtrix `3.0.2`, then you should be able to provide the original image as the source for the directions, along with the shell you’re interested in, i.e. something like this:

```auto
sh2amp sh_coeffs.nii.gz data.nii.gz -fslgrad bvecs bvals -shells 1000 out.nii.gz

```

Hopefully that’ll work and provide consistent results…

---

<div class="post-metadata">

**Author:** ![mlyon](https://community.mrtrix.org/user_avatar/community.mrtrix.org/mlyon/32/2046_2.png) [@mlyon](https://community.mrtrix.org/u/mlyon)\
**Post date:** [October 6, 2021, 3:24pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/14 "2021-10-06T15:24:36Z")

</div>

Thanks @jdtournier for the quick and in-depth response! This is certainly not the first time that I’ve been caught out by the x-axis flip 😅

The command you suggested did work as expected. Although `sh2amp` did complain that `-shells` was an unknown option; not too much of a problem though as you can easily export a single shell using `mrconvert`.

---

<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:** [October 6, 2021, 3:37pm UTC](https://community.mrtrix.org/t/spherical-harmonic-coeffs-to-dwi-images/2014/15 "2021-10-06T15:37:21Z")

</div>

> [@mlyon](#):
>
> Although `sh2amp` did complain that `-shells` was an unknown option;

Oops, you’re right, my mistake…

> [@mlyon](#):
>
> not too much of a problem though as you can easily export a single shell using `mrconvert`.

Indeed, though I’d personally recommend using [dwiextract](https://mrtrix.readthedocs.io/en/latest/reference/commands/dwiextract.html) for that particular task…
