Test I/O from and to *.annot files.
(tmp_path)
| 364 | |
| 365 | @testing.requires_testing_data |
| 366 | def test_annot_io(tmp_path): |
| 367 | """Test I/O from and to *.annot files.""" |
| 368 | # copy necessary files from fsaverage to tempdir |
| 369 | pytest.importorskip("nibabel") |
| 370 | subject = "fsaverage" |
| 371 | label_src = subjects_dir / "fsaverage" / "label" |
| 372 | surf_src = subjects_dir / "fsaverage" / "surf" |
| 373 | label_dir = tmp_path / subject / "label" |
| 374 | surf_dir = tmp_path / subject / "surf" |
| 375 | os.makedirs(label_dir) |
| 376 | os.mkdir(surf_dir) |
| 377 | shutil.copy(label_src / "lh.PALS_B12_Lobes.annot", label_dir) |
| 378 | shutil.copy(label_src / "rh.PALS_B12_Lobes.annot", label_dir) |
| 379 | shutil.copy(surf_src / "lh.white", surf_dir) |
| 380 | shutil.copy(surf_src / "rh.white", surf_dir) |
| 381 | |
| 382 | # read original labels |
| 383 | with pytest.raises(OSError, match="\nPALS_B12_Lobes$"): |
| 384 | read_labels_from_annot(subject, "PALS_B12_Lobesey", subjects_dir=tmp_path) |
| 385 | labels = read_labels_from_annot(subject, "PALS_B12_Lobes", subjects_dir=tmp_path) |
| 386 | |
| 387 | # test saving parcellation only covering one hemisphere |
| 388 | parc = [label for label in labels if label.name == "LOBE.TEMPORAL-lh"] |
| 389 | write_labels_to_annot(parc, subject, "myparc", subjects_dir=tmp_path) |
| 390 | parc1 = read_labels_from_annot(subject, "myparc", subjects_dir=tmp_path) |
| 391 | parc1 = [label for label in parc1 if not label.name.startswith("unknown")] |
| 392 | assert_equal(len(parc1), len(parc)) |
| 393 | for lt, rt in zip(parc1, parc): |
| 394 | assert_labels_equal(lt, rt) |
| 395 | |
| 396 | # test saving only one hemisphere |
| 397 | parc = [label for label in labels if label.name.startswith("LOBE")] |
| 398 | write_labels_to_annot(parc, subject, "myparc2", hemi="lh", subjects_dir=tmp_path) |
| 399 | annot_fname = tmp_path / subject / "label" / "%sh.myparc2.annot" |
| 400 | assert Path(str(annot_fname) % "l").is_file() |
| 401 | assert not Path(str(annot_fname) % "r").is_file() |
| 402 | parc1 = read_labels_from_annot( |
| 403 | subject, "myparc2", annot_fname=str(annot_fname) % "l", subjects_dir=tmp_path |
| 404 | ) |
| 405 | parc_lh = [label for label in parc if label.name.endswith("lh")] |
| 406 | for lt, rt in zip(parc1, parc_lh): |
| 407 | assert_labels_equal(lt, rt) |
| 408 | |
| 409 | # test that the annotation is complete (test Label() support) |
| 410 | rr = read_surface(surf_dir / "lh.white")[0] |
| 411 | label = sum(labels, Label(hemi="lh", subject="fsaverage")).lh |
| 412 | assert_array_equal(label.vertices, np.arange(len(rr))) |
| 413 | |
| 414 | |
| 415 | @testing.requires_testing_data |
nothing calls this directly
no test coverage detected