Skip to content

Commit 4f38b11

Browse files
committed
TEST: Verify that data round trips in column-major order
1 parent 5ac0db6 commit 4f38b11

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

niworkflows/interfaces/tests/test_cifti.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
from pathlib import Path
3+
from unittest import mock
34

45
import nibabel as nb
56
import numpy as np
67
import pytest
78

8-
from ..cifti import GenerateCifti, CIFTI_STRUCT_WITH_LABELS
9+
from ..cifti import GenerateCifti, CIFTI_STRUCT_WITH_LABELS, _create_cifti_image
910

1011

1112
@pytest.fixture(scope="module")
@@ -56,3 +57,34 @@ def test_GenerateCifti(tmpdir, cifti_data):
5657
assert 'SpatialReference' in metadata
5758
for key in ('VolumeReference', 'CIFTI_STRUCTURE_LEFT_CORTEX', 'CIFTI_STRUCTURE_RIGHT_CORTEX'):
5859
assert key in metadata['SpatialReference']
60+
61+
62+
def test__create_cifti_image(tmp_path):
63+
bold_data = np.arange(8, dtype='f4').reshape((2, 2, 2, 1), order='F')
64+
LAS = [[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
65+
bold_img = nb.Nifti1Image(bold_data, LAS)
66+
label_img = nb.Nifti1Image(np.full((2, 2, 2), 16, 'u1'), LAS)
67+
68+
bold_file = tmp_path / 'bold.nii'
69+
volume_label = tmp_path / 'label.nii'
70+
bold_img.to_filename(bold_file)
71+
label_img.to_filename(volume_label)
72+
73+
# Only add one structure to the CIFTI file
74+
with mock.patch(
75+
'niworkflows.interfaces.cifti.CIFTI_STRUCT_WITH_LABELS',
76+
{'CIFTI_STRUCTURE_BRAIN_STEM': (16,)},
77+
):
78+
dummy_fnames = ('', '')
79+
cifti_file = _create_cifti_image(bold_file, volume_label, dummy_fnames, dummy_fnames, 2.0)
80+
81+
cimg = nb.load(cifti_file)
82+
series, bm = [cimg.header.get_axis(i) for i in (0, 1)]
83+
assert len(series) == 1 # Time
84+
assert len(bm) == 8 # Voxel
85+
86+
# Maintaining Fortran ordering, data comes out as it went in
87+
assert np.array_equal(cimg.get_fdata(), bold_data.reshape((1, 8), order='F'))
88+
89+
# Brain model voxels are indexed in Fortran order (fastest first)
90+
assert np.array_equal(bm.voxel[:4], [[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]])

0 commit comments

Comments
 (0)