Skip to content

Commit f0f238d

Browse files
committed
ENH: Implement CoordinateAxis. and Parcel.__getitem__
1 parent 182ff1f commit f0f238d

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

nibabel/coordimage.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from nibabel.fileslice import fill_slicer
2+
3+
14
class CoordinateImage:
25
"""
36
Attributes
@@ -6,6 +9,10 @@ class CoordinateImage:
69
coordaxis : ``CoordinateAxis``
710
dataobj : array-like
811
"""
12+
def __init__(self, data, coordaxis, header=None):
13+
self.data = data
14+
self.coordaxis = coordaxis
15+
self.header = header
916

1017

1118
class CoordinateAxis:
@@ -14,6 +21,8 @@ class CoordinateAxis:
1421
----------
1522
parcels : list of ``Parcel`` objects
1623
"""
24+
def __init__(self, parcels):
25+
self.parcels = parcels
1726

1827
def load_structures(self, mapping):
1928
"""
@@ -26,7 +35,31 @@ def __getitem__(self, slicer):
2635
Return a sub-sampled CoordinateAxis containing structures
2736
matching the indices provided.
2837
"""
29-
raise NotImplementedError
38+
if slicer is Ellipsis or slicer == slice(None):
39+
return self
40+
elif isinstance(slicer, slice):
41+
slicer = fill_slicer(slicer, len(self))
42+
print(slicer)
43+
start, stop, step = slicer.start, slicer.stop, slicer.step
44+
else:
45+
raise TypeError(f"Indexing type not supported: {type(slicer)}")
46+
47+
subparcels = []
48+
pstop = 0
49+
for parcel in self.parcels:
50+
pstart, pstop = pstop, pstop + len(parcel)
51+
print(pstart, pstop)
52+
if pstop < start:
53+
continue
54+
if pstart >= stop:
55+
break
56+
if start < pstart:
57+
substart = (start - pstart) % step
58+
else:
59+
substart = start - pstart
60+
print(slice(substart, stop - pstart, step))
61+
subparcels.append(parcel[substart:stop - pstop:step])
62+
return CoordinateAxis(subparcels)
3063

3164
def get_indices(self, parcel, indices=None):
3265
"""
@@ -36,6 +69,9 @@ def get_indices(self, parcel, indices=None):
3669
"""
3770
raise NotImplementedError
3871

72+
def __len__(self):
73+
return sum(len(parcel) for parcel in self.parcels)
74+
3975

4076
class Parcel:
4177
"""
@@ -45,6 +81,19 @@ class Parcel:
4581
structure : ``Pointset``
4682
indices : object that selects a subset of coordinates in structure
4783
"""
84+
def __init__(self, name, structure, indices):
85+
self.name = name
86+
self.structure = structure
87+
self.indices = indices
88+
89+
def __repr__(self):
90+
return f"<Parcel {self.name}({len(self.indices)})>"
91+
92+
def __len__(self):
93+
return len(self.indices)
94+
95+
def __getitem__(self, slicer):
96+
return self.__class__(self.name, self.structure, self.indices[slicer])
4897

4998

5099
class GeometryCollection:

0 commit comments

Comments
 (0)