|
1 | 1 | import json
|
2 | 2 | from pathlib import Path
|
| 3 | +from unittest import mock |
3 | 4 |
|
4 | 5 | import nibabel as nb
|
5 | 6 | import numpy as np
|
6 | 7 | import pytest
|
7 | 8 |
|
8 |
| -from ..cifti import GenerateCifti, CIFTI_STRUCT_WITH_LABELS |
| 9 | +from ..cifti import GenerateCifti, CIFTI_STRUCT_WITH_LABELS, _create_cifti_image |
9 | 10 |
|
10 | 11 |
|
11 | 12 | @pytest.fixture(scope="module")
|
@@ -56,3 +57,34 @@ def test_GenerateCifti(tmpdir, cifti_data):
|
56 | 57 | assert 'SpatialReference' in metadata
|
57 | 58 | for key in ('VolumeReference', 'CIFTI_STRUCTURE_LEFT_CORTEX', 'CIFTI_STRUCTURE_RIGHT_CORTEX'):
|
58 | 59 | 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