Skip to content

Commit cbb91d1

Browse files
committed
RF: Allow coordinate names to be set on init
1 parent be05f09 commit cbb91d1

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Diff for: nibabel/pointset.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,21 @@ def from_tuple(
169169
mesh: tuple[CoordinateArray, CoordinateArray],
170170
affine: np.ndarray | None = None,
171171
homogeneous: bool = False,
172+
**kwargs,
172173
) -> Self:
173-
return cls(mesh[0], mesh[1], affine=affine, homogeneous=homogeneous)
174+
return cls(mesh[0], mesh[1], affine=affine, homogeneous=homogeneous, **kwargs)
174175

175176
@classmethod
176177
def from_object(
177178
cls,
178179
mesh: HasMeshAttrs,
179180
affine: np.ndarray | None = None,
180181
homogeneous: bool = False,
182+
**kwargs,
181183
) -> Self:
182-
return cls(mesh.coordinates, mesh.triangles, affine=affine, homogeneous=homogeneous)
184+
return cls(
185+
mesh.coordinates, mesh.triangles, affine=affine, homogeneous=homogeneous, **kwargs
186+
)
183187

184188
@property
185189
def n_triangles(self):
@@ -198,9 +202,10 @@ def get_mesh(self, *, as_homogeneous: bool = False):
198202

199203

200204
class CoordinateFamilyMixin(Pointset):
201-
def __init__(self, *args, **kwargs):
202-
self._coords = {}
205+
def __init__(self, *args, name='original', **kwargs):
206+
mapping = kwargs.pop('mapping', {})
203207
super().__init__(*args, **kwargs)
208+
self._coords = {name: self.coordinates, **mapping}
204209

205210
def get_names(self):
206211
"""List of surface names that can be passed to :meth:`with_name`"""

Diff for: nibabel/tests/test_pointset.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def __getitem__(self, slicer):
262262
return h5f[self.dataset_name][slicer]
263263

264264

265-
class H5Geometry(ps.TriangularMesh, ps.CoordinateFamilyMixin):
265+
class H5Geometry(ps.CoordinateFamilyMixin, ps.TriangularMesh):
266266
"""Simple Geometry file structure that combines a single topology
267267
with one or more coordinate sets
268268
"""
@@ -274,8 +274,7 @@ def from_filename(klass, pathlike):
274274
triangles = H5ArrayProxy(pathlike, '/topology')
275275
for name in h5f['coordinates']:
276276
coords[name] = H5ArrayProxy(pathlike, f'/coordinates/{name}')
277-
self = klass(next(iter(coords.values())), triangles)
278-
self._coords.update(coords)
277+
self = klass(next(iter(coords.values())), triangles, mapping=coords)
279278
return self
280279

281280
def to_filename(self, pathlike):
@@ -336,13 +335,12 @@ def triangles(self):
336335
)
337336

338337

339-
class FreeSurferHemisphere(ps.TriangularMesh, ps.CoordinateFamilyMixin):
338+
class FreeSurferHemisphere(ps.CoordinateFamilyMixin, ps.TriangularMesh):
340339
@classmethod
341340
def from_filename(klass, pathlike):
342341
path = Path(pathlike)
343342
hemi, default = path.name.split('.')
344-
self = klass.from_object(FSGeometryProxy(path))
345-
self._coords[default] = self.coordinates
343+
self = klass.from_object(FSGeometryProxy(path), name=default)
346344
mesh_names = (
347345
'orig',
348346
'white',
@@ -353,10 +351,12 @@ def from_filename(klass, pathlike):
353351
'midthickness',
354352
'graymid',
355353
) # Often created
354+
356355
for mesh in mesh_names:
357-
fpath = path.parent / f'{hemi}.{mesh}'
358-
if mesh not in self._coords and fpath.exists():
359-
self.add_coordinates(mesh, FSGeometryProxy(fpath).coordinates)
356+
if mesh != default:
357+
fpath = path.parent / f'{hemi}.{mesh}'
358+
if fpath.exists():
359+
self.add_coordinates(mesh, FSGeometryProxy(fpath).coordinates)
360360
return self
361361

362362

0 commit comments

Comments
 (0)