question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Affine transform between torchio.ScalarImage and nibabel.load do not match

See original GitHub issue

🐛Bug I am using the following NIfTI volume that is part of a dataset that I am using (don’t worry, this is public data and not confidential). When you use torchio.ScalarImage and examine the affine transform after it has been loaded, it is not the same as if you were to use nibabel.load. This causes havoc when I use the ToCanonicaltransform because the volume is not oriented correctly.

To reproduce

Please download the file from the link above. Once you do, do the following:

import torchio as tio
import nibabel as nib

vol_path = 'img0009.nii.gz'
img_s = tio.ScalarImage(vol_path)
print(img_s.affine)
# array([[-0.97656202,  0.        ,  0.        ,  0.        ],
#       [ 0.        , -0.97656202,  0.        ,  0.        ],
#       [ 0.        ,  0.        ,  2.5       ,  0.        ],
#       [ 0.        ,  0.        ,  0.        ,  1.        ]])
img_n = nib.load(vol_path)
print(img_n.affine)
# array([[  -0.97656202,    0.        ,    0.        ,  249.51159692],
#       [   0.        ,    0.97656202,    0.        , -249.51159692],
#       [   0.        ,    0.        ,    2.5       , -185.        ],
#       [   0.        ,    0.        ,    0.        ,    1.        ]])

I am using Nibabel version 3.2.1 and torchio version 0.18.29.

Expected behavior

I would expect the affine transforms between the calls to torchio.ScalarImage and nib.load to be exactly the same. Why is the affine transform modified?

Actual behavior

The affine transforms are different.

System info

Platform: Linux-4.15.0-91-generic-x86_64-with-debian-buster-sid
TorchIO:  0.18.29
PyTorch:  1.7.1
NumPy:    1.19.2
Python:   3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0]

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rayryengcommented, Feb 17, 2021

I got it! Thank you so much! I shall close this.

0reactions
fepegarcommented, Feb 17, 2021

No worries! Actually, TorchIO uses NiBabel only if SimpleITK fails to read the file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image - TorchIO - Read the Docs
An instance of Image can be created using a filepath, a PyTorch tensor, or a NumPy array. This class uses lazy loading, i.e.,...
Read more >
Custom image loader · Issue #437 · fepegar/torchio - GitHub
How can I write my own customer image loader for TorchIO? ... which takes a path and returns a 4D PyTorch tensor and...
Read more >
Issues · fepegar/torchio · GitHub
Affine transform between torchio.ScalarImage and nibabel.load do not match bug Something isn't working. #469 by rayryeng was closed Feb 17, 2021.
Read more >
NiBabel - Coordinate systems and affines - Nipy
The affine matrix as a transformation between spaces¶. We have voxel coordinates (in voxel space). We want to get scanner RAS+ coordinates corresponding...
Read more >
TorchIO: A Python library for efficient loading ... - arXiv
TorchIO transforms can be easily composed, repro- ... This class uses lazy loading, i.e., the data is not loaded from.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found