Skip to content

Commit 9ec4943

Browse files
committed
TEST: Add FreeSurferSubject geometry collection, test loading Cifti2 as cimg
1 parent d0cbc11 commit 9ec4943

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

nibabel/arrayproxy.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ def __init__(self, file_like, spec, *, mmap=True, order=None, keep_file_open=Non
217217
)
218218
self._lock = RLock()
219219

220+
def copy(self):
221+
spec = self._shape, self._dtype, self._offset, self._slope, self._inter
222+
return ArrayProxy(
223+
self.file_like,
224+
spec,
225+
mmap=self._mmap,
226+
keep_file_open=self._keep_file_open,
227+
)
228+
220229
def __del__(self):
221230
"""If this ``ArrayProxy`` was created with ``keep_file_open=True``,
222231
the open file object is closed if necessary.

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)