Load a surf and use the subdivided icosahedron to get points.
(surf, hemi, subject, stype, ico_surf, subjects_dir)
| 1270 | |
| 1271 | |
| 1272 | def _create_surf_spacing(surf, hemi, subject, stype, ico_surf, subjects_dir): |
| 1273 | """Load a surf and use the subdivided icosahedron to get points.""" |
| 1274 | # Based on load_source_space_surf_spacing() in load_source_space.c |
| 1275 | surf = read_surface(surf, return_dict=True)[-1] |
| 1276 | do_neighbor_vert = stype == "spacing" |
| 1277 | complete_surface_info(surf, do_neighbor_vert, copy=False) |
| 1278 | if stype == "all": |
| 1279 | surf["inuse"] = np.ones(surf["np"], int) |
| 1280 | surf["use_tris"] = None |
| 1281 | elif stype == "spacing": |
| 1282 | _decimate_surface_spacing(surf, ico_surf) |
| 1283 | surf["use_tris"] = None |
| 1284 | del surf["neighbor_vert"] |
| 1285 | else: # ico or oct |
| 1286 | # ## from mne_ico_downsample.c ## # |
| 1287 | surf_name = subjects_dir / subject / "surf" / f"{hemi}.sphere" |
| 1288 | logger.info(f"Loading geometry from {surf_name}...") |
| 1289 | from_surf = read_surface(surf_name, return_dict=True)[-1] |
| 1290 | _normalize_vectors(from_surf["rr"]) |
| 1291 | if from_surf["np"] != surf["np"]: |
| 1292 | raise RuntimeError( |
| 1293 | "Mismatch between number of surface vertices, " |
| 1294 | "possible parcellation error?" |
| 1295 | ) |
| 1296 | _normalize_vectors(ico_surf["rr"]) |
| 1297 | |
| 1298 | # Make the maps |
| 1299 | mmap = _compute_nearest(from_surf["rr"], ico_surf["rr"]) |
| 1300 | nmap = len(mmap) |
| 1301 | surf["inuse"] = np.zeros(surf["np"], int) |
| 1302 | for k in range(nmap): |
| 1303 | if surf["inuse"][mmap[k]]: |
| 1304 | # Try the nearest neighbors |
| 1305 | neigh = _get_surf_neighbors(surf, mmap[k]) |
| 1306 | was = mmap[k] |
| 1307 | inds = np.where(np.logical_not(surf["inuse"][neigh]))[0] |
| 1308 | if len(inds) == 0: |
| 1309 | raise RuntimeError( |
| 1310 | f"Could not find neighbor for vertex {k} / {nmap}." |
| 1311 | ) |
| 1312 | else: |
| 1313 | mmap[k] = neigh[inds[-1]] |
| 1314 | logger.info( |
| 1315 | " Source space vertex moved from %d to %d " |
| 1316 | "because of double occupation", |
| 1317 | was, |
| 1318 | mmap[k], |
| 1319 | ) |
| 1320 | elif mmap[k] < 0 or mmap[k] > surf["np"]: |
| 1321 | raise RuntimeError( |
| 1322 | f"Map number out of range ({mmap[k]}), this is probably due to " |
| 1323 | "inconsistent surfaces. Parts of the FreeSurfer reconstruction " |
| 1324 | "need to be redone." |
| 1325 | ) |
| 1326 | surf["inuse"][mmap[k]] = True |
| 1327 | |
| 1328 | logger.info("Setting up the triangulation for the decimated surface...") |
| 1329 | surf["use_tris"] = np.array([mmap[ist] for ist in ico_surf["tris"]], np.int32) |
no test coverage detected