Skip to content

Commit 8a54ade

Browse files
committed
TEST: Add FreeSurferSubject geometry collection, test loading Cifti2 as cimg
1 parent 8926c32 commit 8a54ade

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

nibabel/arrayproxy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ def __init__(self, file_like, spec, *, mmap=True, order=None, keep_file_open=Non
173173
self._should_keep_file_open(file_like, keep_file_open)
174174
self._lock = RLock()
175175

176+
def copy(self):
177+
spec = self._shape, self._dtype, self._offset, self._slope, self._inter
178+
return ArrayProxy(self.file_like, spec,
179+
mmap=self._mmap, keep_file_open=self._keep_file_open)
180+
176181
def __del__(self):
177182
"""If this ``ArrayProxy`` was created with ``keep_file_open=True``,
178183
the open file object is closed if necessary.

nibabel/tests/test_coordimage.py

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

0 commit comments

Comments
 (0)