@@ -213,7 +213,7 @@ def __array__(self, dtype=None):
213
213
with h5 .File (self .file_like , 'r' ) as h5f :
214
214
return np .asanyarray (h5f [self .dataset_name ], dtype )
215
215
216
- def __slicer__ (self , slicer ):
216
+ def __getitem__ (self , slicer ):
217
217
with h5 .File (self .file_like , 'r' ) as h5f :
218
218
return h5f [self .dataset_name ][slicer ]
219
219
@@ -223,19 +223,22 @@ class H5Geometry(ps.TriangularMesh):
223
223
with one or more coordinate sets
224
224
"""
225
225
226
+ def __init__ (self , meshes ):
227
+ self ._meshes = meshes
228
+
226
229
@classmethod
227
230
def from_filename (klass , pathlike ):
228
231
meshes = {}
229
232
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 )
233
236
return klass (meshes )
234
237
235
238
def to_filename (self , pathlike ):
236
239
topology = None
237
240
coordinates = {}
238
- for name , mesh in self .meshes .items ():
241
+ for name , mesh in self ._meshes .items ():
239
242
coords , faces = mesh
240
243
if topology is None :
241
244
topology = faces
@@ -244,9 +247,9 @@ def to_filename(self, pathlike):
244
247
coordinates [name ] = coords
245
248
246
249
with h5 .File (pathlike , 'w' ) as h5f :
247
- h5f .create_dataset ('/topology' , topology )
250
+ h5f .create_dataset ('/topology' , data = topology )
248
251
for name , coord in coordinates .items ():
249
- h5f .create_dataset (f'/coordinates/{ name } ' , coord )
252
+ h5f .create_dataset (f'/coordinates/{ name } ' , data = coord )
250
253
251
254
def get_coords (self , name = None ):
252
255
if name is None :
@@ -314,6 +317,9 @@ def triangles(self):
314
317
315
318
316
319
class FreeSurferHemisphere (ps .TriangularMesh ):
320
+ def __init__ (self , meshes ):
321
+ self ._meshes = meshes
322
+
317
323
@classmethod
318
324
def from_filename (klass , pathlike ):
319
325
path = Path (pathlike )
@@ -351,8 +357,26 @@ def get_triangles(self, name=None):
351
357
352
358
@property
353
359
def n_coords (self ):
354
- return self .meshes [self ._default ].vnum
360
+ return self ._meshes [self ._default ].vnum
355
361
356
362
@property
357
363
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