Skip to content

Commit f0c9c67

Browse files
committed
TEST: Read FreeSurfer subject, save/read H5 subject
1 parent 74f0112 commit f0c9c67

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

nibabel/tests/test_pointset.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def __array__(self, dtype=None):
213213
with h5.File(self.file_like, 'r') as h5f:
214214
return np.asanyarray(h5f[self.dataset_name], dtype)
215215

216-
def __slicer__(self, slicer):
216+
def __getitem__(self, slicer):
217217
with h5.File(self.file_like, 'r') as h5f:
218218
return h5f[self.dataset_name][slicer]
219219

@@ -223,19 +223,22 @@ class H5Geometry(ps.TriangularMesh):
223223
with one or more coordinate sets
224224
"""
225225

226+
def __init__(self, meshes):
227+
self._meshes = meshes
228+
226229
@classmethod
227230
def from_filename(klass, pathlike):
228231
meshes = {}
229232
with h5.File(pathlike, 'r') as h5f:
230-
triangles = h5f['topology']
231-
for name, coords in h5f['coordinates'].items():
232-
meshes[name] = (coords, triangles)
233+
triangles = H5ArrayProxy(pathlike, '/topology')
234+
for name in h5f['coordinates']:
235+
meshes[name] = (H5ArrayProxy(pathlike, f'/coordinates/{name}'), triangles)
233236
return klass(meshes)
234237

235238
def to_filename(self, pathlike):
236239
topology = None
237240
coordinates = {}
238-
for name, mesh in self.meshes.items():
241+
for name, mesh in self._meshes.items():
239242
coords, faces = mesh
240243
if topology is None:
241244
topology = faces
@@ -244,9 +247,9 @@ def to_filename(self, pathlike):
244247
coordinates[name] = coords
245248

246249
with h5.File(pathlike, 'w') as h5f:
247-
h5f.create_dataset('/topology', topology)
250+
h5f.create_dataset('/topology', data=topology)
248251
for name, coord in coordinates.items():
249-
h5f.create_dataset(f'/coordinates/{name}', coord)
252+
h5f.create_dataset(f'/coordinates/{name}', data=coord)
250253

251254
def get_coords(self, name=None):
252255
if name is None:
@@ -314,6 +317,9 @@ def triangles(self):
314317

315318

316319
class FreeSurferHemisphere(ps.TriangularMesh):
320+
def __init__(self, meshes):
321+
self._meshes = meshes
322+
317323
@classmethod
318324
def from_filename(klass, pathlike):
319325
path = Path(pathlike)
@@ -351,8 +357,26 @@ def get_triangles(self, name=None):
351357

352358
@property
353359
def n_coords(self):
354-
return self.meshes[self._default].vnum
360+
return self._meshes[self._default].vnum
355361

356362
@property
357363
def n_triangles(self):
358-
return self.meshes[self._default].fnum
364+
return self._meshes[self._default].fnum
365+
366+
367+
def test_FreeSurferHemisphere():
368+
lh = FreeSurferHemisphere.from_filename(FS_DATA / 'fsaverage/surf/lh.white')
369+
assert lh.n_coords == 163842
370+
assert lh.n_triangles == 327680
371+
372+
373+
@skipUnless(has_h5py, reason='Test requires h5py')
374+
def test_make_H5Geometry(tmp_path):
375+
lh = FreeSurferHemisphere.from_filename(FS_DATA / 'fsaverage/surf/lh.white')
376+
h5geo = H5Geometry({name: lh.get_mesh(name) for name in ('white', 'pial')})
377+
h5geo.to_filename(tmp_path / 'geometry.h5')
378+
379+
rt_h5geo = H5Geometry.from_filename(tmp_path / 'geometry.h5')
380+
assert set(h5geo._meshes) == set(rt_h5geo._meshes)
381+
assert np.array_equal(lh.get_coords('white'), rt_h5geo.get_coords('white'))
382+
assert np.array_equal(lh.get_triangles(), rt_h5geo.get_triangles())

0 commit comments

Comments
 (0)