Load a FreeSurfer binary patch file. Parameters ---------- fname : str The filename. Returns ------- rrs : ndarray, shape (n_vertices, 3) The points. tris : ndarray, shape (n_tris, 3) The patches. Not all vertices will be present.
(fname)
| 1076 | |
| 1077 | |
| 1078 | def _read_patch(fname): |
| 1079 | """Load a FreeSurfer binary patch file. |
| 1080 | |
| 1081 | Parameters |
| 1082 | ---------- |
| 1083 | fname : str |
| 1084 | The filename. |
| 1085 | |
| 1086 | Returns |
| 1087 | ------- |
| 1088 | rrs : ndarray, shape (n_vertices, 3) |
| 1089 | The points. |
| 1090 | tris : ndarray, shape (n_tris, 3) |
| 1091 | The patches. Not all vertices will be present. |
| 1092 | """ |
| 1093 | # This is adapted from PySurfer PR #269, Bruce Fischl's read_patch.m, |
| 1094 | # and PyCortex (BSD) |
| 1095 | patch = dict() |
| 1096 | with open(fname) as fid: |
| 1097 | ver = np.fromfile(fid, dtype=">i4", count=1).item() |
| 1098 | if ver != -1: |
| 1099 | raise RuntimeError(f"incorrect version # {ver} (not -1) found") |
| 1100 | npts = np.fromfile(fid, dtype=">i4", count=1).item() |
| 1101 | dtype = np.dtype([("vertno", ">i4"), ("x", ">f"), ("y", ">f"), ("z", ">f")]) |
| 1102 | recs = np.fromfile(fid, dtype=dtype, count=npts) |
| 1103 | # numpy to dict |
| 1104 | patch = {key: recs[key] for key in dtype.fields.keys()} |
| 1105 | patch["vertno"] -= 1 |
| 1106 | |
| 1107 | # read surrogate surface |
| 1108 | rrs, tris = read_surface( |
| 1109 | op.join(op.dirname(fname), op.basename(fname)[:3] + "sphere") |
| 1110 | ) |
| 1111 | orig_tris = tris |
| 1112 | is_vert = patch["vertno"] > 0 # negative are edges, ignored for now |
| 1113 | verts = patch["vertno"][is_vert] |
| 1114 | |
| 1115 | # eliminate invalid tris and zero out unused rrs |
| 1116 | mask = np.zeros((len(rrs),), dtype=bool) |
| 1117 | mask[verts] = True |
| 1118 | rrs[~mask] = 0.0 |
| 1119 | tris = tris[mask[tris].all(1)] |
| 1120 | for ii, key in enumerate(["x", "y", "z"]): |
| 1121 | rrs[verts, ii] = patch[key][is_vert] |
| 1122 | return rrs, tris, orig_tris |
| 1123 | |
| 1124 | |
| 1125 | ############################################################################## |