Test writing FreeSurfer parcellation from labels.
(tmp_path)
| 558 | |
| 559 | @testing.requires_testing_data |
| 560 | def test_write_labels_to_annot(tmp_path): |
| 561 | """Test writing FreeSurfer parcellation from labels.""" |
| 562 | pytest.importorskip("nibabel") |
| 563 | labels = read_labels_from_annot("sample", subjects_dir=subjects_dir) |
| 564 | |
| 565 | # create temporary subjects-dir skeleton |
| 566 | surf_dir = subjects_dir / "sample" / "surf" |
| 567 | temp_surf_dir = tmp_path / "sample" / "surf" |
| 568 | os.makedirs(temp_surf_dir) |
| 569 | shutil.copy(surf_dir / "lh.white", temp_surf_dir) |
| 570 | shutil.copy(surf_dir / "rh.white", temp_surf_dir) |
| 571 | os.makedirs(tmp_path / "sample" / "label") |
| 572 | |
| 573 | # test automatic filenames |
| 574 | dst = tmp_path / "sample" / "label" / "%s.%s.annot" |
| 575 | write_labels_to_annot(labels, "sample", "test1", subjects_dir=tmp_path) |
| 576 | assert Path(str(dst) % ("lh", "test1")).exists() |
| 577 | assert Path(str(dst) % ("rh", "test1")).exists() |
| 578 | # lh only |
| 579 | for label in labels: |
| 580 | if label.hemi == "lh": |
| 581 | break |
| 582 | write_labels_to_annot([label], "sample", "test2", subjects_dir=tmp_path) |
| 583 | assert Path(str(dst) % ("lh", "test2")).exists() |
| 584 | assert Path(str(dst) % ("rh", "test2")).exists() |
| 585 | # rh only |
| 586 | for label in labels: |
| 587 | if label.hemi == "rh": |
| 588 | break |
| 589 | write_labels_to_annot([label], "sample", "test3", subjects_dir=tmp_path) |
| 590 | assert Path(str(dst) % ("lh", "test3")).exists() |
| 591 | assert Path(str(dst) % ("rh", "test3")).exists() |
| 592 | # label alone |
| 593 | pytest.raises( |
| 594 | TypeError, |
| 595 | write_labels_to_annot, |
| 596 | labels[0], |
| 597 | "sample", |
| 598 | "test4", |
| 599 | subjects_dir=tmp_path, |
| 600 | ) |
| 601 | |
| 602 | # write left and right hemi labels with filenames: |
| 603 | fnames = [tmp_path / (hemi + "-myparc") for hemi in ["lh", "rh"]] |
| 604 | for fname in fnames: |
| 605 | with pytest.warns(RuntimeWarning, match="subjects_dir"): |
| 606 | write_labels_to_annot(labels, annot_fname=fname) |
| 607 | |
| 608 | # read it back |
| 609 | labels2 = read_labels_from_annot( |
| 610 | "sample", subjects_dir=subjects_dir, annot_fname=fnames[0] |
| 611 | ) |
| 612 | labels22 = read_labels_from_annot( |
| 613 | "sample", subjects_dir=subjects_dir, annot_fname=fnames[1] |
| 614 | ) |
| 615 | labels2.extend(labels22) |
| 616 | |
| 617 | names = [label.name for label in labels2] |
nothing calls this directly
no test coverage detected