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, seed: int | None = None, 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
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 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 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 | |
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
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 1043 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 | |
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
peak_signed ¶
The value of largest magnitude, sign kept (-0.99 beats +0.5).
The peak of a negative-correlation cluster is its most negative r;
np.max would pick the value closest to zero, the weakest one.
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).
Welch's t with Welch-Satterthwaite df, as scipy.stats.ttest_ind(equal_var=False).
A group of one subject has no variance estimate, so Welch's t is undefined; that design
keeps the pooled-variance Student t (:func:unpaired_statistic_name says which ran).
Source code in tit/stats/engine.py
unpaired_statistic_name ¶
The voxel statistic :func:ttest_ind computes for these group sizes, for reports.
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
check_paired_sizes ¶
Raise unless a paired test has as many first-group as second-group subjects.
exclude_degenerate ¶
Drop tests whose t is not finite; return (t, p, degenerate).
Zero within-group variance gives nan (identical constant groups) or
+/-inf (perfect separation). Neither carries a usable cluster mass, so
the caller removes the degenerate entries from its valid mask; here they
become the null-effect pair (0, 1) so no inf/nan can reach a cluster.
Source code in tit/stats/engine.py
no_testable_voxel_error ¶
The error for an empty valid mask, saying which of its two causes it is.
Every reduction over values[valid_mask] downstream would otherwise raise
"zero-size array to reduction operation", a numpy traceback that says
nothing about why. n_with_data is the number of voxels (or vertices) that
had data before the degenerate ones were excluded.
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
resolve_seed ¶
The permutation seed a run uses: seed, or a fresh 32-bit one when it is None.
Drawn here (not left to the generator) so the run can write it down and be reproduced; 32 bits keep it exact in JSON readers that hold numbers as doubles.
Source code in tit/stats/engine.py
significant_cluster_table ¶
The engine's significant clusters, with MNI centres, most extreme first.
sig_clusters are the sign-separated clusters that passed the permutation
test (from :meth:PermutationEngine.correct_groups /
:meth:~PermutationEngine.correct_correlation). Relabelling the union of
their voxels instead (as :func:cluster_analysis does) fuses touching
clusters of opposite sign and loses each one's statistic and p-value.
Each row keeps the engine's id as cluster_id plus size,
stat_value (signed mass, or size), p_value, center_voxel,
center_mni (affine applied to the voxel centroid) and, for a
correlation, mean_r / peak_r. Rows are ordered by |stat_value|
(the extremeness the permutation test ranks on; clusters are
sign-homogeneous), largest first.
Source code in tit/stats/engine.py
cluster_analysis ¶
Connected-component analysis with MNI coordinate mapping.
Returns a list of cluster dicts sorted by size (descending).