engine
tit.stats.engine ¶
Core statistical computation engine for cluster-based permutation testing.
Provides vectorised implementations of voxelwise t-tests, correlations, cluster-based permutation correction, connected-component cluster analysis, and MNE-style p-value computation.
All orchestration functions accept an optional logger.
If None the module-level logger (tit.stats.engine) is used.
See Also¶
tit.stats.permutation : High-level orchestration that drives this engine. tit.stats.config : Configuration dataclasses consumed by the engine.
PermutationEngine ¶
PermutationEngine(*, cluster_threshold: float = 0.05, n_permutations: int = 1000, alpha: float = 0.05, cluster_stat: str = 'mass', alternative: str = 'two-sided', n_jobs: int = -1, log: Logger | None = None)
Master class for cluster-based permutation testing.
Stores all control parameters as self.* so methods only need data arrays.
Source code in tit/stats/engine.py
correct_groups ¶
correct_groups(responders, non_responders, *, p_values, t_statistics, valid_mask, test_type: str = 'unpaired', perm_log_file: str | None = None, subject_ids_resp: list | None = None, subject_ids_non_resp: list | None = None) -> tuple
Cluster-based permutation correction for group comparison.
Returns (sig_mask, threshold, sig_clusters, null_dist, observed_clusters,
correlation_data).
Source code in tit/stats/engine.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 723 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 793 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 | |
correct_correlation ¶
correct_correlation(subject_data, effect_sizes, *, r_values, t_statistics, p_values, valid_mask, correlation_type: str = 'pearson', weights=None, perm_log_file: str | None = None, subject_ids: list | None = None) -> tuple
Cluster-based permutation correction for correlation analysis.
Returns (sig_mask, threshold, sig_clusters, null_dist, observed_clusters,
correlation_data).
Source code in tit/stats/engine.py
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 888 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 | |
pval_from_histogram ¶
Compute per-cluster p-values from a permutation null distribution.
Implements the MNE-Python approach based on Maris & Oostenveld (2007).
Parameters¶
observed_stats : array-like
Observed cluster-level statistics.
null_distribution : array-like
Max-cluster statistics from each permutation.
tail : {0, 1, -1}, optional
Tail of the test: 0 for two-sided, 1 for greater, -1 for less.
sampled : bool, optional
True (default) when null_distribution is a random sample of
m permutations rather than the exhaustive enumeration of the
permutation group. The p-value is then the unbiased, valid estimator
(b + 1) / (m + 1) of Phipson & Smyth (2010), where b counts the
permutations at least as extreme as the observation. Adding the
observed statistic to its own null is what makes the test exact: the
naive b / m can return p = 0, which is not a valid p-value and
makes the test anti-conservative at the very tail where cluster
inference lives. Pass sampled=False only when null_distribution
really is the complete enumeration of every possible relabelling, in
which case b / m is exact.
Returns¶
numpy.ndarray
P-values, one per observed cluster. With sampled=True the smallest
attainable p-value is 1 / (m + 1), never zero.
References¶
Maris, E. & Oostenveld, R. (2007). J. Neurosci. Methods 164(1), 177-190. Phipson, B. & Smyth, G. K. (2010). Stat. Appl. Genet. Mol. Biol. 9(1), 39.
Source code in tit/stats/engine.py
tail_from_alternative ¶
label_signed ¶
Connected components that never merge voxels of opposite t sign.
scipy.ndimage.label on a bare significance mask happily fuses a
positive and a negative supra-threshold blob that happen to touch; the
signed cluster mass of the fused object is then the difference of two
real effects and can be ~0. Cluster inference requires clusters to be
sign-homogeneous, so positive and negative voxels are labelled separately
and the negative labels offset past the positive ones.
One-sided alternatives keep only the relevant sign, matching the
correlation path and MNE's tail=+/-1 behaviour.
Returns (labelled_array, n_clusters).
Source code in tit/stats/engine.py
tail_statistic ¶
Orient the cluster statistic so that larger is more extreme.
The null distribution is a distribution of maxima, so observed and permuted statistics must be measured on the same, monotone "extremeness" scale:
size -- always non-negative; clusters are already sign-restricted, so
the raw size is the extremeness.
mass -- signed. tail=+1 uses the mass, tail=-1 its negation,
tail=0 its absolute value.
Taking a plain max() of signed masses under a left or two-sided tail
(the v2.x behaviour) selects the mass closest to zero among negative
clusters, i.e. the least extreme one, and so produces a null that is far
too small.
Source code in tit/stats/engine.py
correlation ¶
correlation(voxel_data, effect_sizes, correlation_type='pearson', weights=None, voxel_data_preranked=False)
Vectorised correlation for all voxels at once.
Returns (r_values, t_values, p_values) – each shape (n_voxels,).
Source code in tit/stats/engine.py
correlation_voxelwise ¶
correlation_voxelwise(subject_data, effect_sizes, weights=None, correlation_type='pearson', log=None)
Voxelwise correlation between E-field and continuous outcome (ACES-style).
Returns (r_values, t_statistics, p_values, valid_mask) – each (x, y, z).
Source code in tit/stats/engine.py
ttest_ind ¶
Vectorised independent-samples t-test. Returns (t_stats, p_values).
Source code in tit/stats/engine.py
ttest_rel ¶
Vectorised paired-samples t-test. Returns (t_stats, p_values).
Source code in tit/stats/engine.py
ttest_voxelwise ¶
ttest_voxelwise(responders, non_responders, test_type='unpaired', alternative='two-sided', log=None)
Voxelwise t-test. Returns (p_values, t_statistics, valid_mask).
Source code in tit/stats/engine.py
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
cluster_analysis ¶
Connected-component analysis with MNI coordinate mapping.
Returns a list of cluster dicts sorted by size (descending).