|
| 1 | +from pathlib import Path |
| 2 | +from unittest import skipUnless |
| 3 | + |
| 4 | +import numpy as np |
| 5 | + |
| 6 | +from nibabel import pointset as ps |
| 7 | +from nibabel.arrayproxy import ArrayProxy |
| 8 | +from nibabel.onetime import auto_attr |
| 9 | +from nibabel.optpkg import optional_package |
| 10 | +from nibabel.tests.nibabel_data import get_nibabel_data |
| 11 | + |
| 12 | +h5, has_h5py, _ = optional_package('h5py') |
| 13 | + |
| 14 | +FS_DATA = Path(get_nibabel_data()) / 'nitest-freesurfer' |
| 15 | + |
| 16 | + |
| 17 | +class H5ArrayProxy: |
| 18 | + def __init__(self, file_like, dataset_name): |
| 19 | + self.file_like = file_like |
| 20 | + self.dataset_name = dataset_name |
| 21 | + with h5.File(file_like, 'r') as h5f: |
| 22 | + arr = h5f[dataset_name] |
| 23 | + self._shape = arr.shape |
| 24 | + self._dtype = arr.dtype |
| 25 | + |
| 26 | + @property |
| 27 | + def is_proxy(self): |
| 28 | + return True |
| 29 | + |
| 30 | + @property |
| 31 | + def shape(self): |
| 32 | + return self._shape |
| 33 | + |
| 34 | + @property |
| 35 | + def ndim(self): |
| 36 | + return len(self.shape) |
| 37 | + |
| 38 | + @property |
| 39 | + def dtype(self): |
| 40 | + return self._dtype |
| 41 | + |
| 42 | + def __array__(self, dtype=None): |
| 43 | + with h5.File(self.file_like, 'r') as h5f: |
| 44 | + return np.asanyarray(h5f[self.dataset_name], dtype) |
| 45 | + |
| 46 | + def __getitem__(self, slicer): |
| 47 | + with h5.File(self.file_like, 'r') as h5f: |
| 48 | + return h5f[self.dataset_name][slicer] |
| 49 | + |
| 50 | + |
| 51 | +class H5Geometry(ps.TriMeshFamily): |
| 52 | + """Simple Geometry file structure that combines a single topology |
| 53 | + with one or more coordinate sets |
| 54 | + """ |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def from_filename(klass, pathlike): |
| 58 | + meshes = {} |
| 59 | + with h5.File(pathlike, 'r') as h5f: |
| 60 | + triangles = H5ArrayProxy(pathlike, '/topology') |
| 61 | + for name in h5f['coordinates']: |
| 62 | + meshes[name] = (H5ArrayProxy(pathlike, f'/coordinates/{name}'), triangles) |
| 63 | + return klass(meshes) |
| 64 | + |
| 65 | + def to_filename(self, pathlike): |
| 66 | + with h5.File(pathlike, 'w') as h5f: |
| 67 | + h5f.create_dataset('/topology', data=self.get_triangles()) |
| 68 | + for name, coord in self._coords.items(): |
| 69 | + h5f.create_dataset(f'/coordinates/{name}', data=coord) |
| 70 | + |
| 71 | + |
| 72 | +class FSGeometryProxy: |
| 73 | + def __init__(self, pathlike): |
| 74 | + self._file_like = str(Path(pathlike)) |
| 75 | + self._offset = None |
| 76 | + self._vnum = None |
| 77 | + self._fnum = None |
| 78 | + |
| 79 | + def _peek(self): |
| 80 | + from nibabel.freesurfer.io import _fread3 |
| 81 | + |
| 82 | + with open(self._file_like, 'rb') as fobj: |
| 83 | + magic = _fread3(fobj) |
| 84 | + if magic != 16777214: |
| 85 | + raise NotImplementedError('Triangle files only!') |
| 86 | + fobj.readline() |
| 87 | + fobj.readline() |
| 88 | + self._vnum = np.fromfile(fobj, '>i4', 1)[0] |
| 89 | + self._fnum = np.fromfile(fobj, '>i4', 1)[0] |
| 90 | + self._offset = fobj.tell() |
| 91 | + |
| 92 | + @property |
| 93 | + def vnum(self): |
| 94 | + if self._vnum is None: |
| 95 | + self._peek() |
| 96 | + return self._vnum |
| 97 | + |
| 98 | + @property |
| 99 | + def fnum(self): |
| 100 | + if self._fnum is None: |
| 101 | + self._peek() |
| 102 | + return self._fnum |
| 103 | + |
| 104 | + @property |
| 105 | + def offset(self): |
| 106 | + if self._offset is None: |
| 107 | + self._peek() |
| 108 | + return self._offset |
| 109 | + |
| 110 | + @auto_attr |
| 111 | + def coords(self): |
| 112 | + ap = ArrayProxy(self._file_like, ((self.vnum, 3), '>f4', self.offset)) |
| 113 | + ap.order = 'C' |
| 114 | + return ap |
| 115 | + |
| 116 | + @auto_attr |
| 117 | + def triangles(self): |
| 118 | + offset = self.offset + 12 * self.vnum |
| 119 | + ap = ArrayProxy(self._file_like, ((self.fnum, 3), '>i4', offset)) |
| 120 | + ap.order = 'C' |
| 121 | + return ap |
| 122 | + |
| 123 | + |
| 124 | +class FreeSurferHemisphere(ps.TriMeshFamily): |
| 125 | + @classmethod |
| 126 | + def from_filename(klass, pathlike): |
| 127 | + path = Path(pathlike) |
| 128 | + hemi, default = path.name.split('.') |
| 129 | + mesh_names = ( |
| 130 | + 'orig', |
| 131 | + 'white', |
| 132 | + 'smoothwm', |
| 133 | + 'pial', |
| 134 | + 'inflated', |
| 135 | + 'sphere', |
| 136 | + 'midthickness', |
| 137 | + 'graymid', |
| 138 | + ) # Often created |
| 139 | + if default not in mesh_names: |
| 140 | + mesh_names.append(default) |
| 141 | + meshes = {} |
| 142 | + for mesh in mesh_names: |
| 143 | + fpath = path.parent / f'{hemi}.{mesh}' |
| 144 | + if fpath.exists(): |
| 145 | + meshes[mesh] = FSGeometryProxy(fpath) |
| 146 | + hemi = klass(meshes) |
| 147 | + hemi._default = default |
| 148 | + return hemi |
| 149 | + |
| 150 | + |
| 151 | +def test_FreeSurferHemisphere(): |
| 152 | + lh = FreeSurferHemisphere.from_filename(FS_DATA / 'fsaverage/surf/lh.white') |
| 153 | + assert lh.n_coords == 163842 |
| 154 | + assert lh.n_triangles == 327680 |
| 155 | + |
| 156 | + |
| 157 | +@skipUnless(has_h5py, reason='Test requires h5py') |
| 158 | +def test_make_H5Geometry(tmp_path): |
| 159 | + lh = FreeSurferHemisphere.from_filename(FS_DATA / 'fsaverage/surf/lh.white') |
| 160 | + h5geo = H5Geometry({name: lh.get_mesh(name) for name in ('white', 'pial')}) |
| 161 | + h5geo.to_filename(tmp_path / 'geometry.h5') |
| 162 | + |
| 163 | + rt_h5geo = H5Geometry.from_filename(tmp_path / 'geometry.h5') |
| 164 | + assert set(h5geo._coords) == set(rt_h5geo._coords) |
| 165 | + assert np.array_equal(lh.get_coords('white'), rt_h5geo.get_coords('white')) |
| 166 | + assert np.array_equal(lh.get_triangles(), rt_h5geo.get_triangles()) |
0 commit comments