@@ -262,19 +262,21 @@ def __getitem__(self, slicer):
262
262
return h5f [self .dataset_name ][slicer ]
263
263
264
264
265
- class H5Geometry (ps .TriMeshFamily ):
265
+ class H5Geometry (ps .TriangularMesh , ps . CoordinateFamilyMixin ):
266
266
"""Simple Geometry file structure that combines a single topology
267
267
with one or more coordinate sets
268
268
"""
269
269
270
270
@classmethod
271
271
def from_filename (klass , pathlike ):
272
- meshes = {}
272
+ coords = {}
273
273
with h5 .File (pathlike , 'r' ) as h5f :
274
274
triangles = H5ArrayProxy (pathlike , '/topology' )
275
275
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
278
280
279
281
def to_filename (self , pathlike ):
280
282
with h5 .File (pathlike , 'w' ) as h5f :
@@ -334,11 +336,13 @@ def triangles(self):
334
336
)
335
337
336
338
337
- class FreeSurferHemisphere (ps .TriMeshFamily ):
339
+ class FreeSurferHemisphere (ps .TriangularMesh , ps . CoordinateFamilyMixin ):
338
340
@classmethod
339
341
def from_filename (klass , pathlike ):
340
342
path = Path (pathlike )
341
343
hemi , default = path .name .split ('.' )
344
+ self = klass .from_object (FSGeometryProxy (path ))
345
+ self ._coords [default ] = self .coordinates
342
346
mesh_names = (
343
347
'orig' ,
344
348
'white' ,
@@ -349,16 +353,11 @@ def from_filename(klass, pathlike):
349
353
'midthickness' ,
350
354
'graymid' ,
351
355
) # Often created
352
- if default not in mesh_names :
353
- mesh_names .append (default )
354
- meshes = {}
355
356
for mesh in mesh_names :
356
357
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
362
361
363
362
364
363
def test_FreeSurferHemisphere ():
@@ -370,10 +369,14 @@ def test_FreeSurferHemisphere():
370
369
@skipUnless (has_h5py , reason = 'Test requires h5py' )
371
370
def test_make_H5Geometry (tmp_path ):
372
371
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 )
374
375
h5geo .to_filename (tmp_path / 'geometry.h5' )
375
376
376
377
rt_h5geo = H5Geometry .from_filename (tmp_path / 'geometry.h5' )
377
378
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
+ )
379
382
assert np .array_equal (lh .get_triangles (), rt_h5geo .get_triangles ())
0 commit comments