Skip to content

Commit 344bfd8

Browse files
committed
TEST: Add FreeSurferSubject geometry collection, test loading Cifti2 as cimg
1 parent 7a6d50c commit 344bfd8

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

nibabel/tests/test_coordimage.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
from pathlib import Path
3+
4+
import nibabel as nb
5+
from nibabel import coordimage as ci
6+
from nibabel import pointset as ps
7+
from nibabel.tests.nibabel_data import get_nibabel_data
8+
9+
from .test_pointset import FreeSurferHemisphere
10+
11+
CIFTI2_DATA = Path(get_nibabel_data()) / 'nitest-cifti2'
12+
13+
14+
class FreeSurferSubject(ci.GeometryCollection):
15+
@classmethod
16+
def from_subject(klass, subject_id, subjects_dir=None):
17+
"""Load a FreeSurfer subject by ID"""
18+
if subjects_dir is None:
19+
subjects_dir = os.environ['SUBJECTS_DIR']
20+
return klass.from_spec(Path(subjects_dir) / subject_id)
21+
22+
@classmethod
23+
def from_spec(klass, pathlike):
24+
"""Load a FreeSurfer subject from its directory structure"""
25+
subject_dir = Path(pathlike)
26+
surfs = subject_dir / 'surf'
27+
structures = {
28+
'lh': FreeSurferHemisphere.from_filename(surfs / 'lh.white'),
29+
'rh': FreeSurferHemisphere.from_filename(surfs / 'rh.white'),
30+
}
31+
subject = super().__init__(structures)
32+
subject._subject_dir = subject_dir
33+
return subject
34+
35+
36+
def test_Cifti2Image_as_CoordImage():
37+
ones = nb.load(CIFTI2_DATA / 'ones.dscalar.nii')
38+
axes = [ones.header.get_axis(i) for i in range(ones.ndim)]
39+
40+
parcels = []
41+
for name, slicer, bma in axes[1].iter_structures():
42+
if bma.volume_shape:
43+
substruct = ps.NdGrid(bma.volume_shape, bma.affine)
44+
indices = bma.voxel
45+
else:
46+
substruct = None
47+
indices = bma.vertex
48+
parcels.append(ci.Parcel(name, None, indices))
49+
caxis = ci.CoordinateAxis(parcels)
50+
dobj = ones.dataobj.copy()
51+
dobj.order = 'C' # Hack for image with BMA as the last axis
52+
cimg = ci.CoordinateImage(dobj, caxis, ones.header)

0 commit comments

Comments
 (0)