forked from neurostuff/NiMARE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_estimator_performance.py
384 lines (335 loc) · 12.4 KB
/
test_estimator_performance.py
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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
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
381
382
383
384
"""Test estimator, kerneltransformer, and multiple comparisons corrector performance."""
import os
from contextlib import ExitStack as does_not_raise
import numpy as np
import pytest
from nimare.correct import FDRCorrector, FWECorrector
from nimare.generate import create_coordinate_dataset
from nimare.meta import ale, kernel, mkda
from nimare.results import MetaResult
from nimare.tests.utils import _check_p_values, _create_signal_mask, _transform_res
from nimare.utils import mm2vox
# set significance levels used for testing.
ALPHA = 0.05
if os.environ.get("CIRCLECI"):
N_CORES = 1
else:
N_CORES = -1
# PRECOMPUTED FIXTURES
# --------------------
##########################################
# random state
##########################################
@pytest.fixture(scope="session")
def random():
"""Set random state for the tests."""
np.random.seed(1939)
##########################################
# simulated dataset(s)
##########################################
@pytest.fixture(
scope="session",
params=[
pytest.param(
{
"foci": 5,
"fwhm": 10.0,
"n_studies": 40,
"sample_size": 30,
"n_noise_foci": 20,
"seed": 1939,
},
id="normal_data",
)
],
)
def simulatedata_cbma(request):
"""Set the simulated CBMA data according to parameters."""
return request.param["fwhm"], create_coordinate_dataset(**request.param)
##########################################
# signal and non-signal masks
##########################################
@pytest.fixture(scope="session")
def signal_masks(simulatedata_cbma):
"""Define masks of signal and non-signal for performance evaluation."""
_, (ground_truth_foci, dataset) = simulatedata_cbma
ground_truth_foci_ijks = [
tuple(mm2vox(focus, dataset.masker.mask_img.affine)) for focus in ground_truth_foci
]
return _create_signal_mask(np.array(ground_truth_foci_ijks), dataset.masker.mask_img)
##########################################
# meta-analysis estimators
##########################################
@pytest.fixture(
scope="session",
params=[
pytest.param(ale.ALE, id="ale"),
pytest.param(mkda.MKDADensity, id="mkda"),
pytest.param(mkda.KDA, id="kda"),
],
)
def meta_est(request):
"""Define meta-analysis estimators for tests."""
return request.param
##########################################
# meta-analysis estimator parameters
##########################################
@pytest.fixture(
scope="session",
params=[
pytest.param({"null_method": "montecarlo", "n_iters": 100}, id="montecarlo"),
pytest.param({"null_method": "approximate"}, id="approximate"),
pytest.param(
{"null_method": "reduced_montecarlo", "n_iters": 100}, id="reduced_montecarlo"
),
],
)
def meta_params(request):
"""Define meta-analysis Estimator parameters for tests."""
return request.param
##########################################
# meta-analysis kernels
##########################################
@pytest.fixture(
scope="session",
params=[
pytest.param(kernel.ALEKernel, id="ale_kernel"),
pytest.param(kernel.MKDAKernel, id="mkda_kernel"),
pytest.param(kernel.KDAKernel, id="kda_kernel"),
],
)
def kern(request):
"""Define kernel transformers for tests."""
return request.param
##########################################
# multiple comparison correctors (testing)
##########################################
@pytest.fixture(
scope="session",
params=[
pytest.param(FWECorrector(method="bonferroni"), id="fwe_bonferroni"),
pytest.param(
FWECorrector(method="montecarlo", voxel_thresh=ALPHA, n_iters=100, n_cores=N_CORES),
id="fwe_montecarlo",
),
pytest.param(FDRCorrector(method="indep", alpha=ALPHA), id="fdr_indep"),
pytest.param(FDRCorrector(method="negcorr", alpha=ALPHA), id="fdr_negcorr"),
],
)
def corr(request):
"""Define multiple comparisons correctors for tests."""
return request.param
##########################################
# multiple comparison correctors (smoke)
##########################################
@pytest.fixture(
scope="session",
params=[
pytest.param(FWECorrector(method="bonferroni"), id="fwe_bonferroni"),
pytest.param(
FWECorrector(method="montecarlo", voxel_thresh=ALPHA, n_iters=2, n_cores=1),
id="fwe_montecarlo",
),
pytest.param(FDRCorrector(method="indep", alpha=ALPHA), id="fdr_indep"),
pytest.param(FDRCorrector(method="negcorr", alpha=ALPHA), id="fdr_negcorr"),
],
)
def corr_small(request):
"""Define multiple comparisons correctors for tests."""
return request.param
###########################################
# all meta-analysis estimator/kernel combos
###########################################
@pytest.fixture(scope="session")
def meta(simulatedata_cbma, meta_est, kern, meta_params):
"""Define estimator/kernel combinations for tests."""
fwhm, (_, _) = simulatedata_cbma
if kern == kernel.KDAKernel or kern == kernel.MKDAKernel:
kern = kern(r=fwhm / 2)
else:
kern = kern()
# instantiate meta-analysis estimator
return meta_est(kern, **meta_params)
###########################################
# meta-analysis estimator results
###########################################
@pytest.fixture(scope="session")
def meta_res(simulatedata_cbma, meta, random):
"""Define estimators for tests."""
_, (_, dataset) = simulatedata_cbma
# CHECK IF META/KERNEL WORK TOGETHER
####################################
meta_expectation = does_not_raise()
with meta_expectation:
res = meta.fit(dataset)
# if creating the result failed (expected), do not continue
if isinstance(meta_expectation, type(pytest.raises(ValueError))):
pytest.xfail("this meta-analysis & kernel combo fails")
# instantiate meta-analysis estimator
return res
###########################################
# corrected results (testing)
###########################################
@pytest.fixture(scope="session")
def meta_cres(meta, meta_res, corr, random):
"""Define corrected results for tests."""
return _transform_res(meta, meta_res, corr)
###########################################
# corrected results (smoke)
###########################################
@pytest.fixture(scope="session")
def meta_cres_small(meta, meta_res, corr_small, random):
"""Define corrected results for tests."""
return _transform_res(meta, meta_res, corr_small)
# --------------
# TEST FUNCTIONS
# --------------
@pytest.mark.performance_smoke
def test_meta_fit_smoke(meta_res):
"""Smoke test for meta-analytic estimator fit."""
assert isinstance(meta_res, MetaResult)
@pytest.mark.performance_estimators
def test_meta_fit_performance(meta_res, signal_masks, simulatedata_cbma):
"""Test meta-analytic estimator fit performance."""
_, (ground_truth_foci, _) = simulatedata_cbma
mask = meta_res.masker.mask_img
ground_truth_foci_ijks = [tuple(mm2vox(focus, mask.affine)) for focus in ground_truth_foci]
sig_idx, nonsig_idx = [
meta_res.masker.transform(img).astype(bool).squeeze() for img in signal_masks
]
# all estimators generate p-values
p_array = meta_res.get_map("p", return_type="array")
# poor performer(s)
if (
isinstance(meta_res.estimator, ale.ALE)
and isinstance(meta_res.estimator.kernel_transformer, kernel.KDAKernel)
and meta_res.estimator.get_params().get("null_method") == "approximate"
):
good_sensitivity = True
good_specificity = False
elif (
isinstance(meta_res.estimator, ale.ALE)
and isinstance(meta_res.estimator.kernel_transformer, kernel.KDAKernel)
and "montecarlo" in meta_res.estimator.get_params().get("null_method")
):
good_sensitivity = False
good_specificity = True
elif (
isinstance(meta_res.estimator, ale.ALE)
and type(meta_res.estimator.kernel_transformer) == kernel.KDAKernel
and "montecarlo" in meta_res.estimator.get_params().get("null_method")
):
good_sensitivity = False
good_specificity = True
elif (
isinstance(meta_res.estimator, ale.ALE)
and type(meta_res.estimator.kernel_transformer) == kernel.KDAKernel
and meta_res.estimator.get_params().get("null_method") == "approximate"
):
good_sensitivity = True
good_specificity = False
elif (
isinstance(meta_res.estimator, mkda.MKDADensity)
and isinstance(meta_res.estimator.kernel_transformer, kernel.ALEKernel)
and meta_res.estimator.get_params().get("null_method") != "reduced_montecarlo"
):
good_sensitivity = False
good_specificity = True
else:
good_sensitivity = True
good_specificity = True
_check_p_values(
p_array,
meta_res.masker,
sig_idx,
nonsig_idx,
ALPHA,
ground_truth_foci_ijks,
n_iters=None,
good_sensitivity=good_sensitivity,
good_specificity=good_specificity,
)
@pytest.mark.performance_smoke
def test_corr_transform_smoke(meta_cres_small):
"""Smoke test for corrector transform."""
assert isinstance(meta_cres_small, MetaResult)
@pytest.mark.performance_correctors
def test_corr_transform_performance(meta_cres, corr, signal_masks, simulatedata_cbma):
"""Test corrector transform performance."""
_, (ground_truth_foci, _) = simulatedata_cbma
mask = meta_cres.masker.mask_img
ground_truth_foci_ijks = [tuple(mm2vox(focus, mask.affine)) for focus in ground_truth_foci]
sig_idx, nonsig_idx = [
meta_cres.masker.transform(img).astype(bool).squeeze() for img in signal_masks
]
p_array = meta_cres.maps.get("p")
if p_array is None or corr.method == "montecarlo":
p_array = 10 ** -meta_cres.maps.get("logp_level-voxel_corr-FWE_method-montecarlo")
n_iters = corr.parameters.get("n_iters")
# ALE with MKDA kernel with montecarlo correction
# combination gives poor performance
if (
isinstance(meta_cres.estimator, ale.ALE)
and isinstance(meta_cres.estimator.kernel_transformer, kernel.MKDAKernel)
and meta_cres.estimator.get_params().get("null_method") == "approximate"
and corr.method != "montecarlo"
):
good_sensitivity = True
good_specificity = False
elif (
isinstance(meta_cres.estimator, ale.ALE)
and isinstance(meta_cres.estimator.kernel_transformer, kernel.MKDAKernel)
and "montecarlo" in meta_cres.estimator.get_params().get("null_method")
):
good_sensitivity = False
good_specificity = True
elif (
isinstance(meta_cres.estimator, ale.ALE)
and isinstance(meta_cres.estimator.kernel_transformer, kernel.MKDAKernel)
and meta_cres.estimator.get_params().get("null_method") == "approximate"
and corr.method == "montecarlo"
):
good_sensitivity = False
good_specificity = True
elif (
isinstance(meta_cres.estimator, ale.ALE)
and type(meta_cres.estimator.kernel_transformer) == kernel.KDAKernel
and (
"montecarlo" in meta_cres.estimator.get_params().get("null_method")
or (
meta_cres.estimator.get_params().get("null_method") == "approximate"
and corr.method == "montecarlo"
)
)
):
good_sensitivity = False
good_specificity = True
elif (
isinstance(meta_cres.estimator, ale.ALE)
and type(meta_cres.estimator.kernel_transformer) == kernel.KDAKernel
and meta_cres.estimator.get_params().get("null_method") == "approximate"
):
good_sensitivity = True
good_specificity = False
elif (
isinstance(meta_cres.estimator, mkda.MKDADensity)
and isinstance(meta_cres.estimator.kernel_transformer, kernel.ALEKernel)
and meta_cres.estimator.get_params().get("null_method") != "reduced_montecarlo"
and corr.method != "montecarlo"
):
good_sensitivity = False
good_specificity = True
else:
good_sensitivity = True
good_specificity = True
_check_p_values(
p_array,
meta_cres.masker,
sig_idx,
nonsig_idx,
ALPHA,
ground_truth_foci_ijks,
n_iters=n_iters,
good_sensitivity=good_sensitivity,
good_specificity=good_specificity,
)