TheZappie
(Victor Savenije)
January 31, 2025, 11:26am
1
3d slicer does not import volumes that I create with the python code below.
What constraint in 3D slicer is causing this?
import numpy as np
import nibabel as nib
from nibabel import affines
data = np.random.normal(size=(100, 100, 100))
# this works, imports into 3D Slicer:
rotation = 4.683045966258175
translation = np.round(np.array([364049.08509937, 5991031.44814528, 0.]))
# this works, imports into 3D Slicer:
rotation = 4.69
translation = np.round(np.array([364049.08509937, 5991031.44814528, 0.]))
# this does not import into 3D Slicer:
rotation = 4.683045966258175
translation = np.array([364049.08509937, 5991031.44814528, 0.])
dxv = 0.05
dyv = 0.05
affine = np.array([[np.cos(rotation) * dxv, -np.sin(rotation) * dyv, 0],
[np.sin(rotation) * dxv, np.cos(rotation) * dyv, 0],
[0, 0, 1]]
)
nii_matrix = affines.from_matvec(affine, translation)
nifti_img = nib.Nifti1Image(data, nii_matrix)
nib.save(nifti_img, r"D:\TEST\test.nii")
I suspect it might have to do with units within 3D slicer. I tested with these settings:
lassoan
(Andras Lasso)
February 3, 2025, 8:24am
2
Probably the non-normalized image position values cause the problem.
Units are not causing the problem: support in Slicer is added to be able to avoid such extreme position values by choosing the most appropriate coordinate system. However, current units implementation has many limitations, so it cannot yet automatically fix such files yet.
I see that you added an issue for this, let’s continue the discussion there:
opened 07:23AM - 03 Feb 25 UTC
## Summary
3d slicer does not import volumes that I create with the python code… below. Posting this here because I believe this is closer to a bug report than a usage question.
What constraint in 3D slicer is causing this issue?
## Steps to reproduce
On importing the files, the volume is not loaded, and a error is produced. [Error.txt](https://github.com/user-attachments/files/18638632/Error.txt)
Produced by the code below
```python
import numpy as np
import nibabel as nib
from nibabel import affines
data = np.random.normal(size=(100, 100, 100))
# this works, imports into 3D Slicer:
rotation = 4.683045966258175
translation = np.round(np.array([364049.08509937, 5991031.44814528, 0.]))
# this works, imports into 3D Slicer:
rotation = 4.69
translation = np.round(np.array([364049.08509937, 5991031.44814528, 0.]))
# this does not import into 3D Slicer:
rotation = 4.683045966258175
translation = np.array([364049.08509937, 5991031.44814528, 0.])
dxv = 0.05
dyv = 0.05
affine = np.array([[np.cos(rotation) * dxv, -np.sin(rotation) * dyv, 0],
[np.sin(rotation) * dxv, np.cos(rotation) * dyv, 0],
[0, 0, 1]]
)
nii_matrix = affines.from_matvec(affine, translation)
nifti_img = nib.Nifti1Image(data, nii_matrix)
nib.save(nifti_img, r"D:\TEST\test.nii")
```
## Environment
- Slicer version: 5.8.0 r33216 / d20ee94
- Operating system: Windows10