Skip to content

Commit 0086302

Browse files
committed
TEST: Rework FreeSurferHemisphere and H5Geometry examples with mixin
1 parent 790399b commit 0086302

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

nibabel/tests/test_pointset.py

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

264264

265-
class H5Geometry(ps.TriMeshFamily):
265+
class H5Geometry(ps.TriangularMesh, ps.CoordinateFamilyMixin):
266266
"""Simple Geometry file structure that combines a single topology
267267
with one or more coordinate sets
268268
"""
269269

270270
@classmethod
271271
def from_filename(klass, pathlike):
272-
meshes = {}
272+
coords = {}
273273
with h5.File(pathlike, 'r') as h5f:
274274
triangles = H5ArrayProxy(pathlike, '/topology')
275275
for name in h5f['coordinates']:
276-
meshes[name] = (H5ArrayProxy(pathlike, f'/coordinates/{name}'), triangles)
277-
return klass(meshes)
276+
coords[name] = H5ArrayProxy(pathlike, f'/coordinates/{name}')
277+
self = klass(next(iter(coords.values())), triangles)
278+
self._coords.update(coords)
279+
return self
278280

279281
def to_filename(self, pathlike):
280282
with h5.File(pathlike, 'w') as h5f:
@@ -334,11 +336,13 @@ def triangles(self):
334336
)
335337

336338

337-
class FreeSurferHemisphere(ps.TriMeshFamily):
339+
class FreeSurferHemisphere(ps.TriangularMesh, ps.CoordinateFamilyMixin):
338340
@classmethod
339341
def from_filename(klass, pathlike):
340342
path = Path(pathlike)
341343
hemi, default = path.name.split('.')
344+
self = klass.from_object(FSGeometryProxy(path))
345+
self._coords[default] = self.coordinates
342346
mesh_names = (
343347
'orig',
344348
'white',
@@ -349,16 +353,11 @@ def from_filename(klass, pathlike):
349353
'midthickness',
350354
'graymid',
351355
) # Often created
352-
if default not in mesh_names:
353-
mesh_names.append(default)
354-
meshes = {}
355356
for mesh in mesh_names:
356357
fpath = path.parent / f'{hemi}.{mesh}'
357-
if fpath.exists():
358-
meshes[mesh] = FSGeometryProxy(fpath)
359-
hemi = klass(meshes)
360-
hemi._default = default
361-
return hemi
358+
if mesh not in self._coords and fpath.exists():
359+
self.add_coordinates(mesh, FSGeometryProxy(fpath).coordinates)
360+
return self
362361

363362

364363
def test_FreeSurferHemisphere():
@@ -370,10 +369,14 @@ def test_FreeSurferHemisphere():
370369
@skipUnless(has_h5py, reason='Test requires h5py')
371370
def test_make_H5Geometry(tmp_path):
372371
lh = FreeSurferHemisphere.from_filename(FS_DATA / 'fsaverage/surf/lh.white')
373-
h5geo = H5Geometry({name: lh.get_mesh(name) for name in ('white', 'pial')})
372+
h5geo = H5Geometry.from_object(lh)
373+
for name in ('white', 'pial'):
374+
h5geo.add_coordinates(name, lh.with_name(name).coordinates)
374375
h5geo.to_filename(tmp_path / 'geometry.h5')
375376

376377
rt_h5geo = H5Geometry.from_filename(tmp_path / 'geometry.h5')
377378
assert set(h5geo._coords) == set(rt_h5geo._coords)
378-
assert np.array_equal(lh.get_coords('white'), rt_h5geo.get_coords('white'))
379+
assert np.array_equal(
380+
lh.with_name('white').get_coords(), rt_h5geo.with_name('white').get_coords()
381+
)
379382
assert np.array_equal(lh.get_triangles(), rt_h5geo.get_triangles())

0 commit comments

Comments
 (0)