# Incorrect volume information when loading .nrrd

**URL:** <https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907>\
**Category:** Support\
**Tags:** nrrd\
**Created:** [January 22, 2024, 2:13pm UTC](https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907 "2024-01-22T14:13:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mns.larsson](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mns.larsson](https://discourse.slicer.org/u/mns.larsson)\
**Post date:** [January 22, 2024, 2:13pm UTC](https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907/1 "2024-01-22T14:13:14Z")

</div>

When I load a .nrrd file I believe the direction matrix under volume information might be incorrect. In my example, the space directions in the .nrrd file is:

`space directions: (1.99900970198094,0.0716907704748626,-1.35699601564168E-07) (-0.0357263945043087,0.996186852455139,-0.079595185816288) (-0.00285263080149889,0.0795440226793289,0.996827244758606)`

,which would by my calculations give  
image spacing 1.9993, 1.0019, 1.0000  
and IJK to RAS Direction Matrix

```auto
-9.99839316e-01, -7.15529825e-02, 1.35699605e-07,
 1.78691748e-02, -9.94272205e-01, 7.95951878e-02,
-1.42679269e-03, 7.93911410e-02, 9.96827270e-01

```

The only calculations I’ve done here is extract the spacing and convert from LPS to RAS

However, slicer shows the following:  
 ![slicer_example](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/d/9d311262054db52d0b7cf5bff436273cf8fc9bb6.png)

I can match the values shown in slicer by transposing the original space direction matrix but the way I interpret the .nrrd-file format specification that should not be necessary.

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 22, 2024, 3:04pm UTC](https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907/2 "2024-01-22T15:04:01Z")

</div>

There is no need for any transpose. The `space directions` field contains the I, J, K vectors in the physical space, which are the columns of the IJK to RAS matrix.

Note that you may find sign differences between the IJK to RAS matrix displayed in Slicer and the `space directions` field in the NRRD file header. NRRD file header can use various physical image coordinate systems, specified by the `space` field. Usually it contains `left-posterior-superior`, which means that the space direction stores IJK to LPS.

---

<div class="post-metadata">

**Author:** ![mns.larsson](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mns.larsson](https://discourse.slicer.org/u/mns.larsson)\
**Post date:** [January 23, 2024, 7:32am UTC](https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907/3 "2024-01-23T07:32:24Z")

</div>

Thank you for the answer!  
I’ve converted the space\_directions matrix to `IJK_toLPS` and then to `IJK_to_RAS`.  
In my application I load an annotations in python and extract some point, I then use the slicer API to plot annotations with points. The points are added using `vtkMRMLMarkupsFiducialNode` which, as I understand, needs coordinates in RAS directly.

If I compare the IJK\_to\_RAS matrixes when loading the .nrrd file with pynrrd they are not consistent (I need to transpose one of them for it to be consistent). This caused an error when plotting the points.

Note that I do not compare the `space_directions` directly with the `IJK_to_RAS` matrix but convert using the code below

> spacing = affine\_to\_spacing(space\_direction\_matrix) # affine\_to\_spacing from MONAI  
> IJK\_to\_LPS = space\_direction\_matrix/spacing  
> IJK\_to\_RAS = np.array([-1, -1, 1]) @ IJK\_to\_LPS

However, looking at the format specification of .nrrd I believe it might be pynrrd that might do the loading incorrectly ([Teem: nrrd: Definition of NRRD File Format](https://teem.sourceforge.net/nrrd/format.html#spacedirections))

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 23, 2024, 3:11pm UTC](https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907/4 "2024-01-23T15:11:39Z")

</div>

It makes sense for pynrrd to store the vectors as they do now, because you want `nrrd.read_header('myimage.nrrd')['space directions'][0]` to return the first vector, `...[1]` the second vector, etc.

However, it was probably not good choice of pynrrd developers to store the vectors in a 2D numpy array, because as your example shows, somebody may be tempted to interpret this matrix as the matrix transform that the nrrd documentation describes. It would have been more clear storing the vectors in a Python list. At this point, probably the best is to clarify this in the pynnrd documentation. If you have time, it would be nice if you could send a pull request to the [pynrrd repository](https://github.com/mhe/pynrrd) to improve its documentation.

---

<div class="post-metadata">

**Author:** ![mns.larsson](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@mns.larsson](https://discourse.slicer.org/u/mns.larsson)\
**Post date:** [January 23, 2024, 3:37pm UTC](https://discourse.slicer.org/t/incorrect-volume-information-when-loading-nrrd/33907/5 "2024-01-23T15:37:31Z")

</div>

Makes sense, thank you for your help!  
I’ll contact the pynrrd people and make them aware that this might be confusing. In this case, I guess it’s how MONAI reads the header and converts to an affine matrix that is incorrect.
