pyfsviz
¶
pyFSViz package.
Python tools for FreeSurfer visualization and QA
Modules:
-
freesurfer–FreeSurfer data.
-
reports–Utility class for generating a config file from a jinja template.
-
stats–Custom nipype interfaces for FreeSurfer stats commands.
Classes:
-
FreeSurfer–Base class for FreeSurfer data.
-
Template–Simplified jinja2 template class from oesteban.
Functions:
-
get_freesurfer_colormap–Generate matplotlib colormap from FreeSurfer LUT.
-
get_parser–Return the CLI argument parser.
-
main–Run the main program.
FreeSurfer
¶
FreeSurfer(freesurfer_home: str | Path | None = None, subjects_dir: str | Path | None = None, log_level: str = 'INFO')
Base class for FreeSurfer data.
Parameters:
-
(freesurfer_home¶str or ``Path`` representing a path to a directory, default:None) –Path corresponding to FREESURFER_HOME env var.
-
(subjects_dir¶str or ``Path`` representing a path to a directory, default:None) –Path corresponding to SUBJECTS_DIR env var.
-
(log_level¶str, default:'INFO') –Logging level (e.g., "INFO", "DEBUG", "WARNING"). Default is "INFO".
Returns:
-
None–
Methods:
-
check_recon_all–Verify that the subject's FreeSurfer recon finished successfully.
-
gen_aparcaseg_plots–Generate parcellation images (aparc & aseg) and return the path to the aparcaseg.png file.
-
gen_batch_reports–Generate HTML reports with images for multiple subjects.
-
gen_group_report–Generate a group report with outlier information for multiple subjects.
-
gen_html_report–Generate html report with FreeSurfer images.
-
gen_surf_plots–Generate pial, inflated, and sulcal images from various viewpoints.
-
gen_tlrc_data–Generate inverse talairach data for report generation.
-
gen_tlrc_report–Generate a before and after report of Talairach registration. (Will also run file generation if needed).
-
get_colormap–Return the colormap for the FreeSurfer data.
-
get_subjects–Return FreeSurfer subjects found in a directory.
-
resolve_groups–Resolve group definitions to subject ID lists from FreeSurfer directories.
-
subject_summary–Collect a compact individual-report summary from the subject tree.
Attributes:
-
freesurfer_home–Path to the FreeSurfer home directory.
-
logger–Logger for the FreeSurfer class.
-
subjects_dir–Path to the subjects directory.
Source code in src/pyfsviz/freesurfer.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
freesurfer_home
instance-attribute
¶
Path to the FreeSurfer home directory.
logger
instance-attribute
¶
Logger for the FreeSurfer class.
subjects_dir
instance-attribute
¶
Path to the subjects directory.
check_recon_all
¶
Verify that the subject's FreeSurfer recon finished successfully.
Source code in src/pyfsviz/freesurfer.py
505 506 507 508 509 510 511 512 513 514 | |
gen_aparcaseg_plots
¶
gen_aparcaseg_plots(subject: str, output_dir: str) -> Path
Generate parcellation images (aparc & aseg) and return the path to the aparcaseg.png file.
Parameters:
Returns:
-
Path(Path) –Path to the aparcaseg.png file.
Examples:
>>> from pyfsviz.freesurfer import FreeSurfer
>>> fs_dir = FreeSurfer(
... freesurfer_home="/opt/freesurfer",
... subjects_dir="/opt/data",
... )
>>> images = fs_dir.gen_aparcaseg_plots("sub-001", "/opt/data/reports/sub-001")
Source code in src/pyfsviz/freesurfer.py
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 | |
gen_batch_reports
¶
gen_batch_reports(output_dir: str | Path, subjects: list[str] | None = None, template: str | None = None, *, gen_images: bool = True, skip_failed: bool = True, skip_existing: bool = False) -> dict[str, Path | Exception]
Generate HTML reports with images for multiple subjects.
This method first generates all required images (TLRC, aparc+aseg, surfaces) and then creates HTML reports for each subject.
Parameters:
-
(output_dir¶str or Path) –Directory where HTML reports will be saved.
-
(subjects¶list[str] or None, default:None) –List of subject IDs to process. If None, processes all subjects in the subjects directory.
-
(template¶str or None, default:None) –HTML template to use. Default is local individual.html.
-
(gen_images¶bool, default:True) –Generate images for each subject. Default is True.
-
(skip_failed¶bool, default:True) –If True, continues processing other subjects if one fails. If False, raises exception on first failure.
-
(skip_existing¶bool, default:False) –If True, skip subjects that already have an HTML report at
{output_dir}/{subject}/{subject}.html. Incomplete subjects (images but no report) are still processed. Default is False.
Returns:
-
dict[str, Path | Exception]–Dictionary mapping subject IDs to either the generated (or existing) HTML file path or the exception that occurred during processing.
Examples:
>>> from pyfsviz.freesurfer import FreeSurfer
>>> fs = FreeSurfer()
>>> results = fs.gen_batch_reports("reports/")
>>> for subject, result in results.items():
... if isinstance(result, Path):
... print(f"Generated report for {subject}: {result}")
... else:
... print(f"Failed to generate report for {subject}: {result}")
Source code in src/pyfsviz/freesurfer.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 | |
gen_group_report
¶
gen_group_report(output_dir: str | Path, subjects: list[str] | None = None, groups: _GroupDefinition | None = None, template: str | None = None, *, sd_threshold: float = 3.0) -> Path
Generate a group report with outlier information for multiple subjects.
Parameters:
-
(output_dir¶str or Path) –Directory where HTML report will be saved.
-
(subjects¶list[str] | None, default:None) –List of subject IDs to process. If None, processes all subjects in the subjects directory.
-
(groups¶_GroupDefinition | None, default:None) –Optional group definitions for between-group box plots. Each group can be a list of subject IDs or a FreeSurfer directory to scan:
["control", "patient"]scanssubjects_dir/controlandsubjects_dir/patient{"control": None, "patient": None}same as above{"control": "/path/to/control_cohort"}scans an explicit path{"control": ["sub-001"]}uses explicit subject IDs
-
(template¶str | None, default:None) –HTML template to use. Default is local group.html.
-
(sd_threshold¶float, default:3.0) –Standard deviation threshold for outlier detection. Default is 3.0.
Returns:
-
Path(Path) –Path to html file.
Source code in src/pyfsviz/freesurfer.py
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | |
gen_html_report
¶
gen_html_report(subject: str, output_dir: str, img_list: list[Path] | None = None, template: str | None = None) -> Path
Generate html report with FreeSurfer images.
Parameters:
-
(subject¶str) –Subject ID.
-
(output_dir¶str) –HTML file name
-
(img_list¶list[Path] | None, default:None) –List of image paths (PNG format).
-
(template¶str | None, default:None) –HTML template to use. Default is local freesurfer.html.
Returns:
-
Path(Path) –Path to html file.
Examples:
>>> from pyfsviz.freesurfer import FreeSurfer
>>> fs_dir = FreeSurfer(
... freesurfer_home="/opt/freesurfer",
... subjects_dir="/opt/data",
... )
>>> report = fs_dir.gen_html_report("sub-001", "/opt/data/reports")
Source code in src/pyfsviz/freesurfer.py
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 | |
gen_surf_plots
¶
gen_surf_plots(subject: str, output_dir: str) -> list[Path]
Generate pial, inflated, and sulcal images from various viewpoints.
Parameters:
Returns:
-
list[Path]:–List of generated PNG images
Examples:
>>> from pyfsviz.freesurfer import FreeSurfer
>>> fs_dir = FreeSurfer(
... freesurfer_home="/opt/freesurfer",
... subjects_dir="/opt/data",
... )
>>> images = fs_dir.gen_surf_plots("sub-001", "/opt/data/reports/sub-001")
Source code in src/pyfsviz/freesurfer.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 | |
gen_tlrc_data
¶
gen_tlrc_data(subject: str, output_dir: str) -> None
Generate inverse talairach data for report generation.
Parameters:
Examples:
>>> from pyfsviz.freesurfer import FreeSurfer
>>> fs_dir = FreeSurfer(
... freesurfer_home="/opt/freesurfer",
... subjects_dir="/opt/data",
... )
>>> fs_dir.gen_tlrc_data("sub-001", "/opt/data/sub-001/mri/transforms")
Source code in src/pyfsviz/freesurfer.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
gen_tlrc_report
¶
gen_tlrc_report(subject: str, output_dir: str, tlrc_dir: str | None = None, *, gen_data: bool = True) -> Path
Generate a before and after report of Talairach registration. (Will also run file generation if needed).
Parameters:
-
(subject¶str) –Subject ID.
-
(output_dir¶str) –Path to SVG output.
-
(gen_data¶bool, default:True) –Generate inverse Talairach data, by default True
-
(tlrc_dir¶str | None, default:None) –Path to output of
gen_tlrc_data. Default is the subject's mri/transforms directory.
Returns:
-
Path(Path) –SVG file generated from the niworkflows SimpleBeforeAfterRPT
Examples:
>>> from pyfsviz.freesurfer import FreeSurfer
>>> fs_dir = FreeSurfer(
... freesurfer_home="/opt/freesurfer",
... subjects_dir="/opt/data",
... )
>>> report = fs_dir.gen_tlrc_report("sub-001", "/opt/data/reports/sub-001")
Source code in src/pyfsviz/freesurfer.py
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
get_colormap
¶
get_colormap() -> ListedColormap
Return the colormap for the FreeSurfer data.
Source code in src/pyfsviz/freesurfer.py
309 310 311 | |
get_subjects
¶
get_subjects(search_dir: str | Path | None = None) -> list[str]
Return FreeSurfer subjects found in a directory.
Parameters:
-
(search_dir¶str | Path | None, default:None) –Directory to scan for subject folders. Defaults to
self.subjects_dir.
Returns:
Source code in src/pyfsviz/freesurfer.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
resolve_groups
¶
Resolve group definitions to subject ID lists from FreeSurfer directories.
Each group can be defined as:
- A list of subject IDs (explicit membership)
Noneor omitted value: scansubjects_dir / group_name- A path string or
Path: scan that directory for subject folders
Parameters:
-
(groups¶_GroupDefinition) –Group names mapped to subject lists or directories, or a list of subdirectory names under
subjects_dir.
Returns:
Source code in src/pyfsviz/freesurfer.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
subject_summary
¶
Collect a compact individual-report summary from the subject tree.
Parameters:
Returns:
Source code in src/pyfsviz/freesurfer.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 | |
Template
¶
Template(template_str: str)
Simplified jinja2 template class from oesteban.
Methods:
-
compile–Generate a string with the replacements.
-
generate_conf–Save the outcome after replacement on the template to file.
Source code in src/pyfsviz/reports.py
47 48 49 50 51 52 53 54 55 56 57 | |
compile
¶
Generate a string with the replacements.
Source code in src/pyfsviz/reports.py
59 60 61 62 | |
generate_conf
¶
Save the outcome after replacement on the template to file.
Source code in src/pyfsviz/reports.py
64 65 66 67 68 | |
get_freesurfer_colormap
¶
get_freesurfer_colormap(freesurfer_home: Path | str) -> ListedColormap
Generate matplotlib colormap from FreeSurfer LUT.
Code from: https://github.com/Deep-MI/fsqc/blob/stable/fsqc/createScreenshots.py
Parameters:
-
(freesurfer_home¶path or str representing a path to a directory) –Path corresponding to FREESURFER_HOME env var.
Returns:
-
colormap(ListedColormap) –A matplotlib compatible FreeSurfer colormap.
Source code in src/pyfsviz/freesurfer.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | |
get_parser
¶
get_parser() -> ArgumentParser
Return the CLI argument parser.
Returns:
-
An argparse parser.–
Source code in src/pyfsviz/_internal/cli.py
19 20 21 22 23 24 25 26 27 28 29 | |
main
¶
Run the main program.
This function is executed when you type pyfsviz or python -m pyfsviz.
Parameters:
Returns:
-
An exit code.–
Source code in src/pyfsviz/_internal/cli.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |