-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_jonckheere.py
More file actions
265 lines (224 loc) · 7.23 KB
/
Copy pathtest_jonckheere.py
File metadata and controls
265 lines (224 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import numpy as np
import pytest
from pybmds.stats.jonckheere import Approach, jonckheere
@pytest.fixture
def valid_x():
return np.linspace(1, 8, 8)
@pytest.fixture
def rep_x():
return np.array([1.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
@pytest.fixture
def valid_group():
return np.repeat(np.linspace(1, 4, 4), 2)
class TestJonckheere:
def test_obs_warning(self, rep_x, valid_group):
with pytest.warns(UserWarning, match="total observations < 30"):
jonckheere(rep_x, valid_group, hypothesis="increasing")
def test_exact_result(self, valid_x, valid_group):
result = jonckheere(valid_x, valid_group, hypothesis="increasing")
assert pytest.approx(result.statistic) == 24.0
assert pytest.approx(result.p_value, abs=1e-3) == 0.00109
result = jonckheere(valid_x, valid_group, hypothesis="two-sided")
assert pytest.approx(result.statistic) == 24.0
assert pytest.approx(result.p_value, abs=1e-4) == 0.000793
def test_valid_result(self, valid_x, valid_group):
result = jonckheere(valid_x, valid_group, hypothesis="increasing")
assert 0 <= result.p_value <= 1
def test_result_table(self, valid_x, valid_group):
result = jonckheere(valid_x, valid_group)
assert isinstance(result.tbl(), str)
@pytest.mark.parametrize("hypothesis", ("increasing", "decreasing", "two-sided"))
def test_hypothesis_paths(self, hypothesis, valid_x, valid_group):
# run all code paths and confirm we always get a p_value.
# unique x
result = jonckheere(valid_x, valid_group, hypothesis=hypothesis)
assert 0 <= result.p_value <= 1
with pytest.warns(
UserWarning,
match="P-value estimated using normal distribution; total observations < 30",
):
result = jonckheere(
np.repeat(np.linspace(1, 10, 10), 2),
np.repeat(np.linspace(1, 5, 5), 4),
hypothesis=hypothesis,
)
assert 0 <= result.p_value <= 1
# permutations
result = jonckheere(valid_x, valid_group, hypothesis=hypothesis, nperm=10)
assert 0 <= result.p_value <= 1
def test_data_not_numeric(self, valid_group):
x = np.array("a b c".split())
with pytest.raises(ValueError, match="Data needs to be numeric"):
jonckheere(x, valid_group, hypothesis="two-sided")
def test_group_not_numeric(self, valid_x):
group = np.array("a b c".split())
with pytest.raises(ValueError, match="Group needs to be numeric or ordered factor"):
jonckheere(valid_x, group, hypothesis="two-sided")
def test_group_data_different_lengths(self, valid_x):
group = np.array([1, 1, 2, 2, 3])
with pytest.raises(ValueError, match="Data and group values need to be the same length"):
jonckheere(valid_x, group, hypothesis="two-sided")
def test_hypothesis_not_valid(self, valid_x, valid_group):
hypothesis = "test"
with pytest.raises(ValueError, match="'test' is not a valid Hypothesis"):
jonckheere(valid_x, valid_group, hypothesis)
def test_data_empty(self, valid_group):
x = np.repeat(np.nan, 8)
with pytest.raises(
ValueError, match="Either data or group is missing for all observations"
):
jonckheere(x, valid_group, hypothesis="two-sided")
def test_group_empty(self, valid_x):
group = np.repeat(np.nan, 8)
with pytest.raises(
ValueError, match="Either data or group is missing for all observations"
):
jonckheere(valid_x, group, hypothesis="two-sided")
def test_one_group(self, valid_x):
group = np.ones(8)
with pytest.raises(ValueError, match="Only one group has non-missing data"):
jonckheere(valid_x, group, hypothesis="two-sided")
def test_exact_increasing_trend_small_pvalue(self):
"""Exact method should give a small p-value for a strong increasing trend."""
x = np.array(
[
1,
2,
3,
10,
11,
12,
20,
21,
22,
],
dtype=float,
)
group = np.array(
[
0,
0,
0,
1,
1,
1,
2,
2,
2,
],
dtype=float,
)
result = jonckheere(x, group, hypothesis="increasing")
assert result.approach == Approach.exact
assert result.p_value < 0.05
def test_exact_decreasing_trend_small_pvalue(self):
"""Exact method should give a small p-value for a strong decreasing trend."""
x = np.array(
[
20,
21,
22,
10,
11,
12,
1,
2,
3,
],
dtype=float,
)
group = np.array(
[
0,
0,
0,
1,
1,
1,
2,
2,
2,
],
dtype=float,
)
result = jonckheere(x, group, hypothesis="decreasing")
assert result.approach == Approach.exact
assert result.p_value < 0.05
def test_permutation_increasing_trend_small_pvalue(self):
"""Permutation method should use the upper tail for an increasing trend."""
x = np.array(
[
1,
2,
3,
10,
11,
12,
20,
21,
22,
],
dtype=float,
)
group = np.array(
[
0,
0,
0,
1,
1,
1,
2,
2,
2,
],
dtype=float,
)
result = jonckheere(
x,
group,
hypothesis="increasing",
nperm=10000,
seed=123,
)
assert result.approach == Approach.permutation
assert result.p_value < 0.05
def test_permutation_decreasing_trend_small_pvalue(self):
"""Permutation method should use the lower tail for a decreasing trend."""
x = np.array(
[
20,
21,
22,
10,
11,
12,
1,
2,
3,
],
dtype=float,
)
group = np.array(
[
0,
0,
0,
1,
1,
1,
2,
2,
2,
],
dtype=float,
)
result = jonckheere(
x,
group,
hypothesis="decreasing",
nperm=10000,
seed=123,
)
assert result.approach == Approach.permutation
assert result.p_value < 0.05