@@ -339,30 +339,27 @@ def _create_cifti_image(
339
339
else :
340
340
model_type = "CIFTI_MODEL_TYPE_VOXELS"
341
341
vox = []
342
- ts = None
342
+ ts = []
343
343
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
346
349
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 ))
357
354
bm = ci .Cifti2BrainModel (
358
355
index_offset = idx_offset ,
359
- index_count = len (vox ),
356
+ index_count = len (vox_indices_ijk ),
360
357
model_type = model_type ,
361
358
brain_structure = structure ,
362
- voxel_indices_ijk = vox ,
359
+ voxel_indices_ijk = vox_indices_ijk ,
363
360
)
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 ))
366
363
# add each brain structure to list
367
364
brainmodels .append (bm )
368
365
0 commit comments