Skip to content

Commit 7a6d50c

Browse files
committed
ENH: Implement CoordinateAxis. and Parcel.__getitem__
1 parent 798f0c6 commit 7a6d50c

File tree

1 file changed

+53
-1
lines changed

1 file changed

+53
-1
lines changed

nibabel/coordimage.py

Lines changed: 53 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
@@ -7,6 +10,11 @@ class CoordinateImage:
710
dataobj : array-like
811
"""
912

13+
def __init__(self, data, coordaxis, header=None):
14+
self.data = data
15+
self.coordaxis = coordaxis
16+
self.header = header
17+
1018

1119
class CoordinateAxis:
1220
"""
@@ -15,6 +23,9 @@ class CoordinateAxis:
1523
parcels : list of ``Parcel`` objects
1624
"""
1725

26+
def __init__(self, parcels):
27+
self.parcels = parcels
28+
1829
def load_structures(self, mapping):
1930
"""
2031
Associate parcels to ``Pointset`` structures
@@ -26,7 +37,31 @@ def __getitem__(self, slicer):
2637
Return a sub-sampled CoordinateAxis containing structures
2738
matching the indices provided.
2839
"""
29-
raise NotImplementedError
40+
if slicer is Ellipsis or slicer == slice(None):
41+
return self
42+
elif isinstance(slicer, slice):
43+
slicer = fill_slicer(slicer, len(self))
44+
print(slicer)
45+
start, stop, step = slicer.start, slicer.stop, slicer.step
46+
else:
47+
raise TypeError(f'Indexing type not supported: {type(slicer)}')
48+
49+
subparcels = []
50+
pstop = 0
51+
for parcel in self.parcels:
52+
pstart, pstop = pstop, pstop + len(parcel)
53+
print(pstart, pstop)
54+
if pstop < start:
55+
continue
56+
if pstart >= stop:
57+
break
58+
if start < pstart:
59+
substart = (start - pstart) % step
60+
else:
61+
substart = start - pstart
62+
print(slice(substart, stop - pstart, step))
63+
subparcels.append(parcel[substart : stop - pstop : step])
64+
return CoordinateAxis(subparcels)
3065

3166
def get_indices(self, parcel, indices=None):
3267
"""
@@ -36,6 +71,9 @@ def get_indices(self, parcel, indices=None):
3671
"""
3772
raise NotImplementedError
3873

74+
def __len__(self):
75+
return sum(len(parcel) for parcel in self.parcels)
76+
3977

4078
class Parcel:
4179
"""
@@ -46,6 +84,20 @@ class Parcel:
4684
indices : object that selects a subset of coordinates in structure
4785
"""
4886

87+
def __init__(self, name, structure, indices):
88+
self.name = name
89+
self.structure = structure
90+
self.indices = indices
91+
92+
def __repr__(self):
93+
return f'<Parcel {self.name}({len(self.indices)})>'
94+
95+
def __len__(self):
96+
return len(self.indices)
97+
98+
def __getitem__(self, slicer):
99+
return self.__class__(self.name, self.structure, self.indices[slicer])
100+
49101

50102
class GeometryCollection:
51103
"""

0 commit comments

Comments
 (0)