Skip to content

Commit 5edddd4

Browse files
committed
ENH: Add CoordinateImage slicing by parcel name
1 parent 1392a06 commit 5edddd4

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Diff for: nibabel/coordimage.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ def __init__(self, data, coordaxis, header=None):
1919
self.coordaxis = coordaxis
2020
self.header = header
2121

22+
@property
23+
def shape(self):
24+
return self.data.shape
25+
26+
def __getitem__(self, slicer):
27+
if isinstance(slicer, str):
28+
slicer = self.coordaxis.get_indices(slicer)
29+
elif isinstance(slicer, list):
30+
slicer = np.hstack([self.coordaxis.get_indices(sub) for sub in slicer])
31+
32+
if isinstance(slicer, range):
33+
slicer = slice(slicer.start, slicer.stop, slicer.step)
34+
35+
data = self.data
36+
if not isinstance(slicer, slice):
37+
data = np.asanyarray(data)
38+
return self.__class__(data[slicer], self.coordaxis[slicer], header=self.header.copy())
39+
2240
@classmethod
2341
def from_image(klass, img):
2442
coordaxis = CoordinateAxis.from_header(img.header)
@@ -57,7 +75,7 @@ def __getitem__(self, slicer):
5775
Return a sub-sampled CoordinateAxis containing structures
5876
matching the indices provided.
5977
"""
60-
if slicer is Ellipsis or slicer == slice(None):
78+
if slicer is Ellipsis or isinstance(slicer, slice) and slicer == slice(None):
6179
return self
6280
elif isinstance(slicer, slice):
6381
slicer = fill_slicer(slicer, len(self))

Diff for: nibabel/tests/test_coordimage.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@ def test_Cifti2Image_as_CoordImage():
7070
assert len(subaxis) == len(caxis) - 200
7171
assert len(subaxis.parcels) == len(caxis.parcels)
7272

73-
caxis.get_indices('CIFTI_STRUCTURE_CORTEX_LEFT')
73+
lh_img = cimg['CIFTI_STRUCTURE_CORTEX_LEFT']
74+
assert len(lh_img.coordaxis.parcels) == 1
75+
assert lh_img.shape == (29696, 1)
76+
77+
# # Not working yet.
78+
# cortex_img = cimg[["CIFTI_STRUCTURE_CORTEX_LEFT", "CIFTI_STRUCTURE_CORTEX_RIGHT"]]
79+
# assert len(cortex_img.coordaxis.parcels) == 2
80+
# assert cortex_img.shape == (59412, 1)

0 commit comments

Comments
 (0)