Skip to content

Commit 5ac0db6

Browse files
committed
FIX: Generate CIFTI volume structure indices in column-major order
1 parent 83bb10e commit 5ac0db6

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

niworkflows/interfaces/cifti.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,30 +339,27 @@ def _create_cifti_image(
339339
else:
340340
model_type = "CIFTI_MODEL_TYPE_VOXELS"
341341
vox = []
342-
ts = None
342+
ts = []
343343
for label in labels:
344-
ijk = np.nonzero(label_data == label)
345-
if ijk[0].size == 0: # skip label if nothing matches
344+
# nonzero returns indices in row-major (C) order
345+
# NIfTI uses column-major (Fortran) order, so HCP generates indices in F order
346+
# Therefore flip the data and label the indices backwards
347+
k, j, i = np.nonzero(label_data.T == label)
348+
if k.size == 0: # skip label if nothing matches
346349
continue
347-
ts = (
348-
bold_data[ijk]
349-
if ts is None
350-
else np.concatenate((ts, bold_data[ijk]))
351-
)
352-
vox += [
353-
[ijk[0][idx], ijk[1][idx], ijk[2][idx]] for idx in range(len(ts))
354-
]
355-
356-
vox = ci.Cifti2VoxelIndicesIJK(vox)
350+
ts.append(bold_data[i, j, k])
351+
vox.append(np.stack([i, j, k]).T)
352+
353+
vox_indices_ijk = ci.Cifti2VoxelIndicesIJK(np.concatenate(vox))
357354
bm = ci.Cifti2BrainModel(
358355
index_offset=idx_offset,
359-
index_count=len(vox),
356+
index_count=len(vox_indices_ijk),
360357
model_type=model_type,
361358
brain_structure=structure,
362-
voxel_indices_ijk=vox,
359+
voxel_indices_ijk=vox_indices_ijk,
363360
)
364-
idx_offset += len(vox)
365-
bm_ts = np.column_stack((bm_ts, ts.T))
361+
idx_offset += len(vox_indices_ijk)
362+
bm_ts = np.column_stack((bm_ts, np.concatenate(ts).T))
366363
# add each brain structure to list
367364
brainmodels.append(bm)
368365

0 commit comments

Comments
 (0)