5
5
6
6
import numpy as np
7
7
8
- from nimare import stats
9
-
10
-
11
- def test_null_to_p ():
12
- """
13
- Test nimare.stats.null_to_p.
14
- """
15
- data = np .arange (1 , 101 )
16
- assert math .isclose (stats .null_to_p (0 , data , "lower" ), 0.01 )
17
- assert math .isclose (stats .null_to_p (0 , data , "upper" ), 0.99 )
18
- assert math .isclose (stats .null_to_p (0 , data , "two" ), 0.01 )
19
- assert math .isclose (stats .null_to_p (5.1 , data , "lower" ), 0.05 )
20
- assert math .isclose (stats .null_to_p (5.1 , data , "upper" ), 0.95 )
21
- assert math .isclose (stats .null_to_p (5.1 , data , "two" ), 0.1 )
22
- assert math .isclose (stats .null_to_p (95.1 , data , "lower" ), 0.95 )
23
- assert math .isclose (stats .null_to_p (95.1 , data , "upper" ), 0.05 )
24
- assert math .isclose (stats .null_to_p (95.1 , data , "two" ), 0.1 )
25
- assert math .isclose (stats .null_to_p (101.1 , data , "lower" ), 0.99 )
26
- assert math .isclose (stats .null_to_p (101.1 , data , "upper" ), 0.01 )
27
- assert math .isclose (stats .null_to_p (101.1 , data , "two" ), 0.01 )
28
-
29
- # modify data to handle edge case
30
- data [98 ] = 100
31
- assert math .isclose (stats .null_to_p (1 , data , "lower" ), 0.01 )
32
- assert math .isclose (stats .null_to_p (1 , data , "upper" ), 0.99 )
33
- assert math .isclose (stats .null_to_p (100.1 , data , "lower" ), 0.99 )
34
- assert math .isclose (stats .null_to_p (100.1 , data , "upper" ), 0.01 )
35
- assert math .isclose (stats .null_to_p (100.1 , data , "two" ), 0.01 )
8
+ from nimare .stats import null_to_p , nullhist_to_p
9
+
10
+
11
+ def test_null_to_p_float ():
12
+ """Test null_to_p with single float input, assuming asymmetric null dist."""
13
+
14
+ null = [- 10 , - 9 , - 9 , - 3 , - 2 , - 1 , - 1 , 0 , 1 , 1 , 1 , 2 , 3 , 3 , 4 , 4 , 7 , 8 , 8 , 9 ]
15
+
16
+ # Two-tailed
17
+ assert math .isclose (null_to_p (0 , null , "two" ), 0.8 )
18
+ assert math .isclose (null_to_p (9 , null , "two" ), 0.1 )
19
+ assert math .isclose (null_to_p (10 , null , "two" ), 0.05 )
20
+ assert math .isclose (null_to_p (- 9 , null , "two" ), 0.3 )
21
+ assert math .isclose (null_to_p (- 10 , null , "two" ), 0.1 )
22
+ # Still 0.05 because minimum valid p-value is 1 / len(null)
23
+ result = null_to_p (20 , null , "two" )
24
+ assert result == null_to_p (- 20 , null , "two" )
25
+ assert math .isclose (result , 0.05 )
26
+
27
+ # Left/lower-tailed
28
+ assert math .isclose (null_to_p (9 , null , "lower" ), 0.95 )
29
+ assert math .isclose (null_to_p (- 9 , null , "lower" ), 0.15 )
30
+ assert math .isclose (null_to_p (0 , null , "lower" ), 0.4 )
31
+
32
+ # Right/upper-tailed
33
+ assert math .isclose (null_to_p (9 , null , "upper" ), 0.05 )
34
+ assert math .isclose (null_to_p (- 9 , null , "upper" ), 0.95 )
35
+ assert math .isclose (null_to_p (0 , null , "upper" ), 0.65 )
36
+
37
+ # Test that 1/n(null) is preserved with extreme values
38
+ nulldist = np .random .normal (size = 10000 )
39
+ assert math .isclose (null_to_p (20 , nulldist , "two" ), 1 / 10000 )
40
+ assert math .isclose (null_to_p (20 , nulldist , "lower" ), 1 - 1 / 10000 )
41
+
42
+
43
+ def test_null_to_p_float_symmetric ():
44
+ """Test null_to_p with single float input, assuming symmetric null dist."""
45
+
46
+ null = [- 10 , - 9 , - 9 , - 3 , - 2 , - 1 , - 1 , 0 , 1 , 1 , 1 , 2 , 3 , 3 , 4 , 4 , 7 , 8 , 8 , 9 ]
47
+
48
+ # Only need to test two-tailed; symmetry is irrelevant for one-tailed
49
+ assert math .isclose (null_to_p (0 , null , "two" , symmetric = True ), 0.95 )
50
+ result = null_to_p (9 , null , "two" , symmetric = True )
51
+ assert result == null_to_p (- 9 , null , "two" , symmetric = True )
52
+ assert math .isclose (result , 0.2 )
53
+ result = null_to_p (10 , null , "two" , symmetric = True )
54
+ assert result == null_to_p (- 10 , null , "two" , symmetric = True )
55
+ assert math .isclose (result , 0.05 )
56
+ # Still 0.05 because minimum valid p-value is 1 / len(null)
57
+ result = null_to_p (20 , null , "two" , symmetric = True )
58
+ assert result == null_to_p (- 20 , null , "two" , symmetric = True )
59
+ assert math .isclose (result , 0.05 )
60
+
61
+
62
+ def test_null_to_p_array ():
63
+ """Test nimare.stats.null_to_p with 1d array input."""
64
+ N = 10000
65
+ nulldist = np .random .normal (size = N )
66
+ t = np .sort (np .random .normal (size = N ))
67
+ p = np .sort (null_to_p (t , nulldist ))
68
+ assert p .shape == (N ,)
69
+ assert (p < 1 ).all ()
70
+ assert (p > 0 ).all ()
71
+ # Resulting distribution should be roughly uniform
72
+ assert np .abs (p .mean () - 0.5 ) < 0.02
73
+ assert np .abs (p .var () - 1 / 12 ) < 0.02
36
74
37
75
38
76
def test_nullhist_to_p ():
@@ -45,14 +83,14 @@ def test_nullhist_to_p():
45
83
histogram_weights [- 1 ] = 0 # last bin is outside range, so there are 100 bins with values
46
84
47
85
# When input is a single value
48
- assert math .isclose (stats . nullhist_to_p (0 , histogram_weights , histogram_bins ), 1.0 )
49
- assert math .isclose (stats . nullhist_to_p (1 , histogram_weights , histogram_bins ), 0.99 )
50
- assert math .isclose (stats . nullhist_to_p (99 , histogram_weights , histogram_bins ), 0.01 )
51
- assert math .isclose (stats . nullhist_to_p (100 , histogram_weights , histogram_bins ), 0.01 )
86
+ assert math .isclose (nullhist_to_p (0 , histogram_weights , histogram_bins ), 1.0 )
87
+ assert math .isclose (nullhist_to_p (1 , histogram_weights , histogram_bins ), 0.99 )
88
+ assert math .isclose (nullhist_to_p (99 , histogram_weights , histogram_bins ), 0.01 )
89
+ assert math .isclose (nullhist_to_p (100 , histogram_weights , histogram_bins ), 0.01 )
52
90
53
91
# When input is an array
54
92
assert np .allclose (
55
- stats . nullhist_to_p ([0 , 1 , 99 , 100 , 101 ], histogram_weights , histogram_bins ),
93
+ nullhist_to_p ([0 , 1 , 99 , 100 , 101 ], histogram_weights , histogram_bins ),
56
94
np .array ([1.0 , 0.99 , 0.01 , 0.01 , 0.01 ]),
57
95
)
58
96
@@ -61,6 +99,6 @@ def test_nullhist_to_p():
61
99
histogram_weights [- 1 , :] = 0 # last bin is outside range, so there are 100 bins with values
62
100
63
101
assert np .allclose (
64
- stats . nullhist_to_p ([0 , 1 , 99 , 100 , 101 ], histogram_weights , histogram_bins ),
102
+ nullhist_to_p ([0 , 1 , 99 , 100 , 101 ], histogram_weights , histogram_bins ),
65
103
np .array ([1.0 , 0.99 , 0.01 , 0.01 , 0.01 ]),
66
104
)
0 commit comments