Skip to content

Commit 8fd8e61

Browse files
authored
Merge pull request #1194 from MichielCottaar/ragged_parcels
BF: Support ragged voxel arrays in ParcelsAxis
2 parents fc9a1c1 + 870f106 commit 8fd8e61

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

nibabel/cifti2/cifti2_axes.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -775,14 +775,9 @@ def __init__(self, name, voxels, vertices, affine=None, volume_shape=None, nvert
775775
maps names of surface elements to integers (not needed for volumetric CIFTI-2 files)
776776
"""
777777
self.name = np.asanyarray(name, dtype='U')
778-
as_array = np.asanyarray(voxels)
779-
if as_array.ndim == 1:
780-
voxels = as_array.astype('object')
781-
else:
782-
voxels = np.empty(len(voxels), dtype='object')
783-
for idx in range(len(voxels)):
784-
voxels[idx] = as_array[idx]
785-
self.voxels = np.asanyarray(voxels, dtype='object')
778+
self.voxels = np.empty(len(voxels), dtype='object')
779+
for idx, vox in enumerate(voxels):
780+
self.voxels[idx] = vox
786781
self.vertices = np.asanyarray(vertices, dtype='object')
787782
self.affine = np.asanyarray(affine) if affine is not None else None
788783
self.volume_shape = volume_shape

nibabel/cifti2/tests/test_axes.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,13 +494,34 @@ def test_parcels():
494494
assert prc != prc_other
495495

496496
# test direct initialisation
497-
axes.ParcelsAxis(
497+
test_parcel = axes.ParcelsAxis(
498498
voxels=[np.ones((3, 2), dtype=int)],
499499
vertices=[{}],
500500
name=['single_voxel'],
501501
affine=np.eye(4),
502502
volume_shape=(2, 3, 4),
503503
)
504+
assert len(test_parcel) == 1
505+
506+
# test direct initialisation with multiple parcels
507+
test_parcel = axes.ParcelsAxis(
508+
voxels=[np.ones((3, 2), dtype=int), np.zeros((3, 2), dtype=int)],
509+
vertices=[{}, {}],
510+
name=['first_parcel', 'second_parcel'],
511+
affine=np.eye(4),
512+
volume_shape=(2, 3, 4),
513+
)
514+
assert len(test_parcel) == 2
515+
516+
# test direct initialisation with ragged voxel/vertices array
517+
test_parcel = axes.ParcelsAxis(
518+
voxels=[np.ones((3, 2), dtype=int), np.zeros((5, 2), dtype=int)],
519+
vertices=[{}, {}],
520+
name=['first_parcel', 'second_parcel'],
521+
affine=np.eye(4),
522+
volume_shape=(2, 3, 4),
523+
)
524+
assert len(test_parcel) == 2
504525

505526
with pytest.raises(ValueError):
506527
axes.ParcelsAxis(

0 commit comments

Comments
 (0)