-
Notifications
You must be signed in to change notification settings - Fork 56
/
mkda.py
1353 lines (1120 loc) · 58.8 KB
/
mkda.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
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
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
"""CBMA methods from the multilevel kernel density analysis (MKDA) family."""
import logging
import nibabel as nib
import numpy as np
import sparse
from joblib import Memory, Parallel, delayed
from pymare.stats import fdr
from scipy import ndimage
from scipy.stats import chi2
from tqdm.auto import tqdm
from nimare import _version
from nimare.meta.cbma.base import CBMAEstimator, PairwiseCBMAEstimator
from nimare.meta.kernel import KDAKernel, MKDAKernel
from nimare.meta.utils import _calculate_cluster_measures
from nimare.stats import null_to_p, one_way, two_way
from nimare.transforms import p_to_z
from nimare.utils import _check_ncores, tqdm_joblib, vox2mm
LGR = logging.getLogger(__name__)
__version__ = _version.get_versions()["version"]
class MKDADensity(CBMAEstimator):
r"""Multilevel kernel density analysis- Density analysis.
The MKDA density method was originally introduced in :footcite:t:`wager2007meta`.
.. versionchanged:: 0.2.1
- New parameters: ``memory`` and ``memory_level`` for memory caching.
.. versionchanged:: 0.0.12
- Use a 4D sparse array for modeled activation maps.
Parameters
----------
kernel_transformer : :obj:`~nimare.meta.kernel.KernelTransformer`, optional
Kernel with which to convolve coordinates from dataset. Default is
:class:`~nimare.meta.kernel.MKDAKernel`.
null_method : {"approximate", "montecarlo"}, optional
Method by which to determine uncorrected p-values. The available options are
======================= =================================================================
"approximate" (default) Build a histogram of summary-statistic values and their
expected frequencies under the assumption of random spatial
associated between studies, via a weighted convolution.
This method is much faster, but slightly less accurate.
"montecarlo" Perform a large number of permutations, in which the coordinates
in the studies are randomly drawn from the Estimator's brain mask
and the full set of resulting summary-statistic values are
incorporated into a null distribution (stored as a histogram for
memory reasons).
This method is must slower, and is only slightly more accurate.
======================= =================================================================
n_iters : int, optional
Number of iterations to use to define the null distribution.
This is only used if ``null_method=="montecarlo"``.
Default is 10000.
memory : instance of :class:`joblib.Memory`, :obj:`str`, or :class:`pathlib.Path`
Used to cache the output of a function. By default, no caching is done.
If a :obj:`str` is given, it is the path to the caching directory.
memory_level : :obj:`int`, default=0
Rough estimator of the amount of memory used by caching.
Higher value means more memory for caching. Zero means no caching.
n_cores : :obj:`int`, optional
Number of cores to use for parallelization.
This is only used if ``null_method=="montecarlo"``.
If <=0, defaults to using all available cores.
Default is 1.
**kwargs
Keyword arguments. Arguments for the kernel_transformer can be assigned
here, with the prefix '\kernel__' in the variable name.
Attributes
----------
masker : :class:`~nilearn.input_data.NiftiMasker` or similar
Masker object.
inputs_ : :obj:`dict`
Inputs to the Estimator. For CBMA estimators, there is only one key: coordinates.
This is an edited version of the dataset's coordinates DataFrame.
null_distributions_ : :obj:`dict` of :class:`numpy.ndarray`
Null distributions for the uncorrected summary-statistic-to-p-value conversion and any
multiple-comparisons correction methods.
Entries are added to this attribute if and when the corresponding method is applied.
If ``null_method == "approximate"``:
- ``histogram_means``: Array of mean value per experiment.
- ``histogram_bins``: Array of bin centers for the null distribution histogram,
ranging from zero to the maximum possible summary statistic value for the Dataset.
- ``histweights_corr-none_method-approximate``: Array of weights for the null
distribution histogram, with one value for each bin in ``histogram_bins``.
If ``null_method == "montecarlo"``:
- ``histogram_bins``: Array of bin centers for the null distribution histogram,
ranging from zero to the maximum possible summary statistic value for the Dataset.
- ``histweights_corr-none_method-montecarlo``: Array of weights for the null
distribution histogram, with one value for each bin in ``histogram_bins``.
These values are derived from the full set of summary statistics from each
iteration of the Monte Carlo procedure.
- ``histweights_level-voxel_corr-fwe_method-montecarlo``: Array of weights for the
voxel-level FWE-correction null distribution, with one value for each bin in
``histogram_bins``. These values are derived from the maximum summary statistic
from each iteration of the Monte Carlo procedure.
If :meth:`correct_fwe_montecarlo` is applied:
- ``values_level-voxel_corr-fwe_method-montecarlo``: The maximum summary statistic
value from each Monte Carlo iteration. An array of shape (n_iters,).
- ``values_desc-size_level-cluster_corr-fwe_method-montecarlo``: The maximum cluster
size from each Monte Carlo iteration. An array of shape (n_iters,).
- ``values_desc-mass_level-cluster_corr-fwe_method-montecarlo``: The maximum cluster
mass from each Monte Carlo iteration. An array of shape (n_iters,).
Notes
-----
The MKDA density algorithm is also implemented in MATLAB at
https://github.com/canlab/Canlab_MKDA_MetaAnalysis.
Available correction methods: :func:`MKDADensity.correct_fwe_montecarlo`
References
----------
.. footbibliography::
"""
def __init__(
self,
kernel_transformer=MKDAKernel,
null_method="approximate",
n_iters=None,
memory=Memory(location=None, verbose=0),
memory_level=0,
n_cores=1,
**kwargs,
):
if not (isinstance(kernel_transformer, MKDAKernel) or kernel_transformer == MKDAKernel):
LGR.warning(
f"The KernelTransformer being used ({kernel_transformer}) is not optimized "
f"for the {type(self).__name__} algorithm. "
"Expect suboptimal performance and beware bugs."
)
# Add kernel transformer attribute and process keyword arguments
super().__init__(
kernel_transformer=kernel_transformer,
memory=memory,
memory_level=memory_level,
**kwargs,
)
self.null_method = null_method
self.n_iters = None if null_method == "approximate" else n_iters or 10000
self.n_cores = _check_ncores(n_cores)
self.dataset = None
def _generate_description(self):
"""Generate a description of the fitted Estimator.
Returns
-------
str
Description of the Estimator.
"""
if self.null_method == "montecarlo":
null_method_str = (
"a Monte Carlo-based null distribution, in which dataset coordinates were "
"randomly drawn from the analysis mask and the full set of ALE values were "
f"retained, using {self.n_iters} iterations"
)
else:
null_method_str = "an approximate null distribution"
description = (
"A multilevel kernel density (MKDA) meta-analysis \\citep{wager2007meta} was "
"performed was performed with NiMARE "
f"{__version__} "
"(RRID:SCR_017398; \\citealt{Salo2023}), using a(n) "
f"{self.kernel_transformer.__class__.__name__.replace('Kernel', '')} kernel. "
f"{self.kernel_transformer._generate_description()} "
f"Summary statistics (OF values) were converted to p-values using {null_method_str}. "
f"The input dataset included {self.inputs_['coordinates'].shape[0]} foci from "
f"{len(self.inputs_['id'])} experiments."
)
return description
def _compute_weights(self, ma_values):
"""Determine experiment-wise weights per the conventional MKDA approach."""
# TODO: Incorporate sample-size and inference metadata extraction and
# merging into df.
# This will need to be distinct from the kernel_transformer-based kind
# done in CBMAEstimator._preprocess_input
ids_df = self.inputs_["coordinates"].groupby("id").first()
n_exp = len(ids_df)
# Default to unit weighting for missing inference or sample size
if "inference" not in ids_df.columns:
ids_df["inference"] = "rfx"
if "sample_size" not in ids_df.columns:
ids_df["sample_size"] = 1.0
n = ids_df["sample_size"].astype(float).values
inf = ids_df["inference"].map({"ffx": 0.75, "rfx": 1.0}).values
weight_vec = n_exp * ((np.sqrt(n) * inf) / np.sum(np.sqrt(n) * inf))
weight_vec = weight_vec[:, None]
assert weight_vec.shape[0] == ma_values.shape[0]
return weight_vec
def _compute_summarystat_est(self, ma_values):
ma_values = ma_values.reshape((ma_values.shape[0], -1))
stat_values = ma_values.T.dot(self.weight_vec_)
if isinstance(ma_values, sparse._coo.core.COO):
# NOTE: This may not work correctly with a non-NiftiMasker.
mask_data = self.masker.mask_img.get_fdata().astype(bool)
stat_values = stat_values[mask_data.reshape(-1)].ravel()
# This is used by _compute_null_approximate
self.__n_mask_voxels = stat_values.shape[0]
else:
# np.array type is used by _compute_null_reduced_montecarlo
stat_values = stat_values.ravel()
return stat_values
def _determine_histogram_bins(self, ma_maps):
"""Determine histogram bins for null distribution methods.
Parameters
----------
ma_maps : :obj:`sparse._coo.core.COO`
MA maps.
The ma_maps can be a 4d sparse array of MA maps,
Notes
-----
This method adds two entries to the null_distributions_ dict attribute: "histogram_bins",
and "histogram_means" only if ``null_method == "approximate"``.
"""
if not isinstance(ma_maps, sparse._coo.core.COO):
raise ValueError(f"Unsupported data type '{type(ma_maps)}'")
n_exp = ma_maps.shape[0]
prop_active = np.zeros(n_exp)
data = ma_maps.data
coords = ma_maps.coords
for exp_idx in range(n_exp):
# The first column of coords is the fourth dimension of the dense array
study_ma_values = data[coords[0, :] == exp_idx]
n_nonzero_voxels = study_ma_values.shape[0]
n_zero_voxels = self.__n_mask_voxels - n_nonzero_voxels
prop_active[exp_idx] = np.mean(np.hstack([study_ma_values, np.zeros(n_zero_voxels)]))
self.null_distributions_["histogram_bins"] = np.arange(len(prop_active) + 1, step=1)
if self.null_method.startswith("approximate"):
# To speed things up in _compute_null_approximate, we save the means too,
self.null_distributions_["histogram_means"] = prop_active
def _compute_null_approximate(self, ma_maps):
"""Compute uncorrected null distribution using approximate solution.
Parameters
----------
ma_maps
Modeled activation maps. Unused for this estimator.
Notes
-----
This method adds one entry to the null_distributions_ dict attribute: "histogram_weights".
"""
assert "histogram_means" in self.null_distributions_.keys()
# MKDA maps are binary, so we only have k + 1 bins in the final
# histogram, where k is the number of studies. We can analytically
# compute the null distribution by convolution.
# prop_active contains the mean value per experiment
prop_active = self.null_distributions_["histogram_means"]
ss_hist = 1.0
for exp_prop in prop_active:
ss_hist = np.convolve(ss_hist, [1 - exp_prop, exp_prop])
self.null_distributions_["histweights_corr-none_method-approximate"] = ss_hist
class MKDAChi2(PairwiseCBMAEstimator):
r"""Multilevel kernel density analysis- Chi-square analysis.
The MKDA chi-square method was originally introduced in :footcite:t:`wager2007meta`.
.. versionchanged:: 0.2.1
- Make `prior` parameter default to None, which controls if posterior probabilities
pFgA, pAgF_prior and pFgA_prior are calculated. This is useful because probability
maps are difficult to interpret and for speeding up the algorithm.
- Rename ``consistency`` to ``uniformity`` and ``specificity`` to ``association`` to match
Neurosynth's terminology
- New parameters: ``memory`` and ``memory_level`` for memory caching.
.. versionchanged:: 0.0.12
- Use a 4D sparse array for modeled activation maps.
.. versionchanged:: 0.0.8
* [REF] Use saved MA maps, when available.
Parameters
----------
kernel_transformer : :obj:`~nimare.meta.kernel.KernelTransformer`, optional
Kernel with which to convolve coordinates from dataset. Default is
:class:`~nimare.meta.kernel.MKDAKernel`.
prior : float, optional
Uniform prior probability of each feature being active in a map in
the absence of evidence from the map. Default: 0.5
memory : instance of :class:`joblib.Memory`, :obj:`str`, or :class:`pathlib.Path`
Used to cache the output of a function. By default, no caching is done.
If a :obj:`str` is given, it is the path to the caching directory.
memory_level : :obj:`int`, default=0
Rough estimator of the amount of memory used by caching.
Higher value means more memory for caching. Zero means no caching.
**kwargs
Keyword arguments. Arguments for the kernel_transformer can be assigned
here, with the prefix '\kernel__' in the variable name.
Attributes
----------
masker : :class:`~nilearn.input_data.NiftiMasker` or similar
Masker object.
inputs_ : :obj:`dict`
Inputs to the Estimator. For CBMA estimators, there is only one key: coordinates.
This is an edited version of the dataset's coordinates DataFrame.
null_distributions_ : :obj:`dict` of :class:`numpy.ndarray`
Null distributions for any multiple-comparisons correction methods.
.. important::
MKDAChi2 does not retain uncorrected summary-statistic-to-p null distributions,
since the summary statistic in this case is the chi-squared value, which has an
established null distribution.
Entries are added to this attribute if and when the corresponding method is applied.
If :meth:`correct_fwe_montecarlo` is applied:
- ``values_desc-pAgF_level-voxel_corr-fwe_method-montecarlo``: The maximum
chi-squared value from the p(A|F) one-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pAgFsize_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster size value from the p(A|F) one-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pAgFmass_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster mass value from the p(A|F) one-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pFgA_level-voxel_corr-fwe_method-montecarlo``: The maximum
chi-squared value from the p(F|A) two-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pFgAsize_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster size value from the p(F|A) two-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pFgAmass_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster mass value from the p(F|A) two-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
Notes
-----
The MKDA Chi-square algorithm was originally implemented as part of the Neurosynth Python
library (https://github.com/neurosynth/neurosynth).
Available correction methods: :meth:`MKDAChi2.correct_fwe_montecarlo`,
:meth:`MKDAChi2.correct_fdr_indep`.
References
----------
.. footbibliography::
"""
def __init__(
self,
kernel_transformer=MKDAKernel(),
prior=0.5,
memory=Memory(location=None, verbose=0),
memory_level=0,
**kwargs,
):
if not (isinstance(kernel_transformer, MKDAKernel) or kernel_transformer == MKDAKernel):
LGR.warning(
f"The KernelTransformer being used ({kernel_transformer}) is not optimized "
f"for the {type(self).__name__} algorithm. "
"Expect suboptimal performance and beware bugs."
)
# Add kernel transformer attribute and process keyword arguments
super().__init__(
kernel_transformer=kernel_transformer,
memory=memory,
memory_level=memory_level,
**kwargs,
)
self.prior = prior
def _generate_description(self):
description = (
"A multilevel kernel density chi-squared analysis \\citep{wager2007meta} was "
"performed according to the same procedure as implemented in Neurosynth with NiMARE "
f"{__version__} "
"(RRID:SCR_017398; \\citealt{Salo2023}), "
f"using a(n) {self.kernel_transformer.__class__.__name__.replace('Kernel', '')} "
"kernel. "
f"{self.kernel_transformer._generate_description()} "
"This analysis calculated several measures. "
"The first dataset was evaluated for uniformity of activation via a one-way "
"chi-square test. "
f"The first input dataset included {self.inputs_['coordinates1'].shape[0]} foci from "
f"{len(self.inputs_['id1'])} experiments. "
f"The second input dataset included {self.inputs_['coordinates2'].shape[0]} foci from "
f"{len(self.inputs_['id2'])} experiments."
)
return description
def _fit(self, dataset1, dataset2):
self.dataset1 = dataset1
self.dataset2 = dataset2
self.masker = self.masker or dataset1.masker
self.null_distributions_ = {}
# Generate MA maps and calculate count variables for first dataset
n_selected_active_voxels = self._collect_ma_maps(
maps_key="ma_maps1",
coords_key="coordinates1",
return_type="summary_array",
)
n_selected = self.dataset1.coordinates["id"].unique().shape[0]
# Generate MA maps and calculate count variables for second dataset
n_unselected_active_voxels = self._collect_ma_maps(
maps_key="ma_maps2",
coords_key="coordinates2",
return_type="summary_array",
)
n_unselected = self.dataset2.coordinates["id"].unique().shape[0]
n_mappables = n_selected + n_unselected
# Nomenclature for variables below: p = probability,
# F = feature present, g = given, U = unselected, A = activation.
# So, e.g., pAgF = p(A|F) = probability of activation
# in a voxel if we know that the feature is present in a study.
pF = n_selected / n_mappables
pA = np.array(
(n_selected_active_voxels + n_unselected_active_voxels) / n_mappables
).squeeze()
del n_mappables
pAgF = n_selected_active_voxels / n_selected
pAgU = n_unselected_active_voxels / n_unselected
pFgA = pAgF * pF / pA
del pF
if self.prior:
# Recompute conditionals with uniform prior
pAgF_prior = self.prior * pAgF + (1 - self.prior) * pAgU
pFgA_prior = pAgF * self.prior / pAgF_prior
# One-way chi-square test for uniformity of activation
pAgF_chi2_vals = one_way(np.squeeze(n_selected_active_voxels), n_selected)
pAgF_p_vals = chi2.sf(pAgF_chi2_vals, 1)
pAgF_sign = np.sign(n_selected_active_voxels - np.mean(n_selected_active_voxels))
pAgF_z = p_to_z(pAgF_p_vals, tail="two") * pAgF_sign
del pAgF_sign
# Two-way chi-square for association of activation
cells = np.squeeze(
np.array(
[
[n_selected_active_voxels, n_unselected_active_voxels],
[
n_selected - n_selected_active_voxels,
n_unselected - n_unselected_active_voxels,
],
]
).T
)
del n_selected, n_unselected
pFgA_chi2_vals = two_way(cells)
del n_selected_active_voxels, n_unselected_active_voxels
eps = np.spacing(1)
pFgA_p_vals = chi2.sf(pFgA_chi2_vals, 1)
pFgA_p_vals[pFgA_p_vals < eps] = eps
pFgA_sign = np.sign(pAgF - pAgU).ravel()
pFgA_z = p_to_z(pFgA_p_vals, tail="two") * pFgA_sign
del pFgA_sign, pAgU
maps = {
"z_desc-uniformity": pAgF_z,
"z_desc-association": pFgA_z,
"chi2_desc-uniformity": pAgF_chi2_vals,
"chi2_desc-association": pFgA_chi2_vals,
"p_desc-uniformity": pAgF_p_vals,
"p_desc-association": pFgA_p_vals,
"prob_desc-A": pA,
"prob_desc-AgF": pAgF,
"prob_desc-FgA": pFgA,
}
if self.prior:
maps["prob_desc-AgF_prior"] = pAgF_prior
maps["prob_desc-FgA_prior"] = pFgA_prior
description = self._generate_description()
return maps, {}, description
def _run_fwe_permutation(self, iter_xyz1, iter_xyz2, iter_df1, iter_df2, conn, voxel_thresh):
"""Run a single permutation of the Monte Carlo FWE correction procedure.
Parameters
----------
iter_xyz1, iter_xyz2 : :obj:`numpy.ndarray`
Random coordinates for the permutation.
iter_df1, iter_df2 : :obj:`pandas.DataFrame`
DataFrames with as many rows as there are coordinates in each of the two datasets,
to be filled in with random coordinates for the permutation.
conn : :obj:`numpy.ndarray` of shape (3, 3, 3)
Connectivity matrix for defining clusters.
voxel_thresh : :obj:`float`
Uncorrected summary-statistic thresholded for defining clusters.
Returns
-------
pAgF_max_chi2_value : :obj:`float`
Forward inference maximum chi-squared value, for voxel-level FWE correction.
pAgF_max_size : :obj:`float`
Forward inference maximum cluster size, for cluster-level FWE correction.
pAgF_max_mass : :obj:`float`
Forward inference maximum cluster mass, for cluster-level FWE correction.
pFgA_max_chi2_value : :obj:`float`
Reverse inference maximum chi-squared value, for voxel-level FWE correction.
pFgA_max_size : :obj:`float`
Reverse inference maximum cluster size, for cluster-level FWE correction.
pFgA_max_mass : :obj:`float`
Reverse inference maximum cluster mass, for cluster-level FWE correction.
"""
# Not sure if joblib will automatically use a copy of the object, but I'll make a copy to
# be safe.
iter_df1 = iter_df1.copy()
iter_df2 = iter_df2.copy()
iter_xyz1 = np.squeeze(iter_xyz1)
iter_xyz2 = np.squeeze(iter_xyz2)
iter_df1[["x", "y", "z"]] = iter_xyz1
iter_df2[["x", "y", "z"]] = iter_xyz2
# Generate MA maps and calculate count variables for first dataset
n_selected_active_voxels = self.kernel_transformer.transform(
iter_df1, self.masker, return_type="summary_array"
)
n_selected = self.dataset1.coordinates["id"].unique().shape[0]
# Generate MA maps and calculate count variables for second dataset
n_unselected_active_voxels = self.kernel_transformer.transform(
iter_df2, self.masker, return_type="summary_array"
)
n_unselected = self.dataset2.coordinates["id"].unique().shape[0]
# Currently unused conditional probabilities
# pAgF = n_selected_active_voxels / n_selected
# pAgU = n_unselected_active_voxels / n_unselected
# One-way chi-square test for uniformity of activation
pAgF_chi2_vals = one_way(np.squeeze(n_selected_active_voxels), n_selected)
# Voxel-level inference
pAgF_max_chi2_value = np.max(np.abs(pAgF_chi2_vals))
# Cluster-level inference
pAgF_chi2_map = self.masker.inverse_transform(pAgF_chi2_vals).get_fdata()
pAgF_max_size, pAgF_max_mass = _calculate_cluster_measures(
pAgF_chi2_map, voxel_thresh, conn, tail="two"
)
# Two-way chi-square for association of activation
cells = np.squeeze(
np.array(
[
[n_selected_active_voxels, n_unselected_active_voxels],
[
n_selected - n_selected_active_voxels,
n_unselected - n_unselected_active_voxels,
],
]
).T
)
pFgA_chi2_vals = two_way(cells)
# Voxel-level inference
pFgA_max_chi2_value = np.max(np.abs(pFgA_chi2_vals))
# Cluster-level inference
pFgA_chi2_map = self.masker.inverse_transform(pFgA_chi2_vals).get_fdata()
pFgA_max_size, pFgA_max_mass = _calculate_cluster_measures(
pFgA_chi2_map, voxel_thresh, conn, tail="two"
)
return (
pAgF_max_chi2_value,
pAgF_max_size,
pAgF_max_mass,
pFgA_max_chi2_value,
pFgA_max_size,
pFgA_max_mass,
)
def _apply_correction(self, stat_values, voxel_thresh, vfwe_null, csfwe_null, cmfwe_null):
"""Apply different kinds of FWE correction to statistical value matrix.
.. versionchanged:: 0.0.13
Change cluster neighborhood from faces+edges to faces, to match Nilearn.
Parameters
----------
stat_values : :obj:`numpy.ndarray`
1D array of summary-statistic values.
voxel_thresh : :obj:`float`
Summary statistic threshold for defining clusters.
vfwe_null, csfwe_null, cmfwe_null : :obj:`numpy.ndarray`
Null distributions for FWE correction.
Returns
-------
p_vfwe_values, p_csfwe_values, p_cmfwe_values : :obj:`numpy.ndarray`
1D arrays of FWE-corrected p-values.
"""
eps = np.spacing(1)
# Define connectivity matrix for cluster labeling
conn = ndimage.generate_binary_structure(rank=3, connectivity=1)
# Voxel-level FWE
p_vfwe_values = null_to_p(np.abs(stat_values), vfwe_null, tail="upper")
# Crop p-values of 0 or 1 to nearest values that won't evaluate to 0 or 1.
# Prevents inf z-values.
p_vfwe_values[p_vfwe_values < eps] = eps
p_vfwe_values[p_vfwe_values > (1.0 - eps)] = 1.0 - eps
# Cluster-level FWE
# Extract the summary statistics in voxel-wise (3D) form, threshold, and cluster-label
stat_map_thresh = self.masker.inverse_transform(stat_values).get_fdata()
stat_map_thresh[np.abs(stat_map_thresh) <= voxel_thresh] = 0
# Label positive and negative clusters separately
labeled_matrix = np.empty(stat_map_thresh.shape, int)
labeled_matrix, _ = ndimage.label(stat_map_thresh > 0, conn)
n_positive_clusters = np.max(labeled_matrix)
temp_labeled_matrix, _ = ndimage.label(stat_map_thresh < 0, conn)
temp_labeled_matrix[temp_labeled_matrix > 0] += n_positive_clusters
labeled_matrix = labeled_matrix + temp_labeled_matrix
del temp_labeled_matrix
cluster_labels, idx, cluster_sizes = np.unique(
labeled_matrix,
return_inverse=True,
return_counts=True,
)
assert cluster_labels[0] == 0
# Cluster mass-based inference
cluster_masses = np.zeros(cluster_labels.shape)
for i_val in cluster_labels:
if i_val == 0:
cluster_masses[i_val] = 0
cluster_mass = np.sum(np.abs(stat_map_thresh[labeled_matrix == i_val]) - voxel_thresh)
cluster_masses[i_val] = cluster_mass
p_cmfwe_vals = null_to_p(cluster_masses, cmfwe_null, tail="upper")
p_cmfwe_map = p_cmfwe_vals[np.reshape(idx, labeled_matrix.shape)]
p_cmfwe_values = np.squeeze(
self.masker.transform(nib.Nifti1Image(p_cmfwe_map, self.masker.mask_img.affine))
)
# Cluster size-based inference
cluster_sizes[0] = 0 # replace background's "cluster size" with zeros
p_csfwe_vals = null_to_p(cluster_sizes, csfwe_null, tail="upper")
p_csfwe_map = p_csfwe_vals[np.reshape(idx, labeled_matrix.shape)]
p_csfwe_values = np.squeeze(
self.masker.transform(nib.Nifti1Image(p_csfwe_map, self.masker.mask_img.affine))
)
return p_vfwe_values, p_csfwe_values, p_cmfwe_values
def correct_fwe_montecarlo(self, result, voxel_thresh=0.001, n_iters=5000, n_cores=1):
"""Perform FWE correction using the max-value permutation method.
Only call this method from within a Corrector.
.. versionchanged:: 0.0.13
Change cluster neighborhood from faces+edges to faces, to match Nilearn.
.. versionchanged:: 0.0.12
Include cluster level-corrected results in Monte Carlo null method.
Parameters
----------
result : :obj:`~nimare.results.MetaResult`
Result object from a KDA meta-analysis.
n_iters : :obj:`int`, optional
Number of iterations to build the vFWE null distribution.
Default is 5000.
n_cores : :obj:`int`, optional
Number of cores to use for parallelization.
If <=0, defaults to using all available cores. Default is 1.
Returns
-------
maps : :obj:`dict`
Dictionary of 1D arrays corresponding to masked maps generated by
the correction procedure. The following arrays are generated by
this method:
- ``p_desc-uniformity_level-voxel``: Voxel-level FWE-corrected p-values from the
uniformity/forward inference analysis.
- ``z_desc-uniformity_level-voxel``: Voxel-level FWE-corrected z-values from the
uniformity/forward inference analysis.
- ``logp_desc-uniformity_level-voxel``: Voxel-level FWE-corrected -log10 p-values
from the uniformity/forward inference analysis.
- ``p_desc-uniformityMass_level-cluster``: Cluster-level FWE-corrected p-values
from the uniformity/forward inference analysis, using cluster mass.
- ``z_desc-uniformityMass_level-cluster``: Cluster-level FWE-corrected z-values
from the uniformity/forward inference analysis, using cluster mass.
- ``logp_desc-uniformityMass_level-cluster``: Cluster-level FWE-corrected -log10
p-values from the uniformity/forward inference analysis, using cluster mass.
- ``p_desc-uniformitySize_level-cluster``: Cluster-level FWE-corrected p-values
from the uniformity/forward inference analysis, using cluster size.
- ``z_desc-uniformitySize_level-cluster``: Cluster-level FWE-corrected z-values
from the uniformity/forward inference analysis, using cluster size.
- ``logp_desc-uniformitySize_level-cluster``: Cluster-level FWE-corrected -log10
p-values from the uniformity/forward inference analysis, using cluster size.
- ``p_desc-association_level-voxel``: Voxel-level FWE-corrected p-values from the
association/reverse inference analysis.
- ``z_desc-association_level-voxel``: Voxel-level FWE-corrected z-values from the
association/reverse inference analysis.
- ``logp_desc-association_level-voxel``: Voxel-level FWE-corrected -log10 p-values
from the association/reverse inference analysis.
- ``p_desc-associationMass_level-cluster``: Cluster-level FWE-corrected p-values
from the association/reverse inference analysis, using cluster mass.
- ``z_desc-associationMass_level-cluster``: Cluster-level FWE-corrected z-values
from the association/reverse inference analysis, using cluster mass.
- ``logp_desc-associationMass_level-cluster``: Cluster-level FWE-corrected -log10
p-values from the association/reverse inference analysis, using cluster mass.
- ``p_desc-associationSize_level-cluster``: Cluster-level FWE-corrected p-values
from the association/reverse inference analysis, using cluster size.
- ``z_desc-associationSize_level-cluster``: Cluster-level FWE-corrected z-values
from the association/reverse inference analysis, using cluster size.
- ``logp_desc-associationSize_level-cluster``: Cluster-level FWE-corrected -log10
p-values from the association/reverse inference analysis, using cluster size.
Notes
-----
This method adds six new keys to the ``null_distributions_`` attribute:
- ``values_desc-pAgF_level-voxel_corr-fwe_method-montecarlo``: The maximum
chi-squared value from the p(A|F) one-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pAgFsize_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster size value from the p(A|F) one-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pAgFmass_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster mass value from the p(A|F) one-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pFgA_level-voxel_corr-fwe_method-montecarlo``: The maximum
chi-squared value from the p(F|A) two-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pFgAsize_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster size value from the p(F|A) two-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
- ``values_desc-pFgAmass_level-cluster_corr-fwe_method-montecarlo``: The maximum
cluster mass value from the p(F|A) two-way chi-squared test from each Monte Carlo
iteration. An array of shape (n_iters,).
See Also
--------
nimare.correct.FWECorrector : The Corrector from which to call this method.
Examples
--------
>>> meta = MKDAChi2()
>>> result = meta.fit(dset)
>>> corrector = FWECorrector(method='montecarlo', n_iters=5, n_cores=1)
>>> cresult = corrector.transform(result)
"""
null_xyz = vox2mm(
np.vstack(np.where(self.masker.mask_img.get_fdata())).T,
self.masker.mask_img.affine,
)
pAgF_chi2_vals = result.get_map("chi2_desc-uniformity", return_type="array")
pFgA_chi2_vals = result.get_map("chi2_desc-association", return_type="array")
pAgF_z_vals = result.get_map("z_desc-uniformity", return_type="array")
pFgA_z_vals = result.get_map("z_desc-association", return_type="array")
pAgF_sign = np.sign(pAgF_z_vals)
pFgA_sign = np.sign(pFgA_z_vals)
n_cores = _check_ncores(n_cores)
iter_df1 = self.inputs_["coordinates1"]
iter_df2 = self.inputs_["coordinates2"]
rand_idx1 = np.random.choice(null_xyz.shape[0], size=(iter_df1.shape[0], n_iters))
rand_xyz1 = null_xyz[rand_idx1, :]
iter_xyzs1 = np.split(rand_xyz1, rand_xyz1.shape[1], axis=1)
rand_idx2 = np.random.choice(null_xyz.shape[0], size=(iter_df2.shape[0], n_iters))
rand_xyz2 = null_xyz[rand_idx2, :]
iter_xyzs2 = np.split(rand_xyz2, rand_xyz2.shape[1], axis=1)
eps = np.spacing(1)
# Identify summary statistic corresponding to intensity threshold
ss_thresh = chi2.isf(voxel_thresh, 1)
# Define connectivity matrix for cluster labeling
conn = ndimage.generate_binary_structure(rank=3, connectivity=1)
with tqdm_joblib(tqdm(total=n_iters)):
perm_results = Parallel(n_jobs=n_cores)(
delayed(self._run_fwe_permutation)(
iter_xyz1=iter_xyzs1[i_iter],
iter_xyz2=iter_xyzs2[i_iter],
iter_df1=iter_df1,
iter_df2=iter_df2,
conn=conn,
voxel_thresh=ss_thresh,
)
for i_iter in range(n_iters)
)
del rand_idx1, rand_xyz1, iter_xyzs1
del rand_idx2, rand_xyz2, iter_xyzs2
(
pAgF_vfwe_null,
pAgF_csfwe_null,
pAgF_cmfwe_null,
pFgA_vfwe_null,
pFgA_csfwe_null,
pFgA_cmfwe_null,
) = zip(*perm_results)
del perm_results
# pAgF_FWE
pAgF_p_vfwe_vals, pAgF_p_csfwe_vals, pAgF_p_cmfwe_vals = self._apply_correction(
pAgF_chi2_vals,
ss_thresh,
vfwe_null=pAgF_vfwe_null,
csfwe_null=pAgF_csfwe_null,
cmfwe_null=pAgF_cmfwe_null,
)
self.null_distributions_[
"values_desc-pAgF_level-voxel_corr-fwe_method-montecarlo"
] = pAgF_vfwe_null
self.null_distributions_[
"values_desc-pAgFsize_level-cluster_corr-fwe_method-montecarlo"
] = pAgF_csfwe_null
self.null_distributions_[
"values_desc-pAgFmass_level-cluster_corr-fwe_method-montecarlo"
] = pAgF_cmfwe_null
del pAgF_vfwe_null, pAgF_csfwe_null, pAgF_cmfwe_null
# pFgA_FWE
pFgA_p_vfwe_vals, pFgA_p_csfwe_vals, pFgA_p_cmfwe_vals = self._apply_correction(
pFgA_chi2_vals,
ss_thresh,
vfwe_null=pFgA_vfwe_null,
csfwe_null=pFgA_csfwe_null,
cmfwe_null=pFgA_cmfwe_null,
)
self.null_distributions_[
"values_desc-pFgA_level-voxel_corr-fwe_method-montecarlo"
] = pFgA_vfwe_null
self.null_distributions_[
"values_desc-pFgAsize_level-cluster_corr-fwe_method-montecarlo"
] = pFgA_csfwe_null
self.null_distributions_[
"values_desc-pFgAmass_level-cluster_corr-fwe_method-montecarlo"
] = pFgA_cmfwe_null
del pFgA_vfwe_null, pFgA_csfwe_null, pFgA_cmfwe_null
# Convert p-values
# pAgF
pAgF_z_vfwe_vals = p_to_z(pAgF_p_vfwe_vals, tail="two") * pAgF_sign
pAgF_logp_vfwe_vals = -np.log10(pAgF_p_vfwe_vals)
pAgF_logp_vfwe_vals[np.isinf(pAgF_logp_vfwe_vals)] = -np.log10(eps)
pAgF_z_cmfwe_vals = p_to_z(pAgF_p_cmfwe_vals, tail="two") * pAgF_sign
pAgF_logp_cmfwe_vals = -np.log10(pAgF_p_cmfwe_vals)
pAgF_logp_cmfwe_vals[np.isinf(pAgF_logp_cmfwe_vals)] = -np.log10(eps)
pAgF_z_csfwe_vals = p_to_z(pAgF_p_csfwe_vals, tail="two") * pAgF_sign
pAgF_logp_csfwe_vals = -np.log10(pAgF_p_csfwe_vals)
pAgF_logp_csfwe_vals[np.isinf(pAgF_logp_csfwe_vals)] = -np.log10(eps)
# pFgA
pFgA_z_vfwe_vals = p_to_z(pFgA_p_vfwe_vals, tail="two") * pFgA_sign
pFgA_logp_vfwe_vals = -np.log10(pFgA_p_vfwe_vals)
pFgA_logp_vfwe_vals[np.isinf(pFgA_logp_vfwe_vals)] = -np.log10(eps)
pFgA_z_cmfwe_vals = p_to_z(pFgA_p_cmfwe_vals, tail="two") * pFgA_sign
pFgA_logp_cmfwe_vals = -np.log10(pFgA_p_cmfwe_vals)
pFgA_logp_cmfwe_vals[np.isinf(pFgA_logp_cmfwe_vals)] = -np.log10(eps)
pFgA_z_csfwe_vals = p_to_z(pFgA_p_csfwe_vals, tail="two") * pFgA_sign
pFgA_logp_csfwe_vals = -np.log10(pFgA_p_csfwe_vals)
pFgA_logp_csfwe_vals[np.isinf(pFgA_logp_csfwe_vals)] = -np.log10(eps)
maps = {
# uniformity analysis
"p_desc-uniformity_level-voxel": pAgF_p_vfwe_vals,
"z_desc-uniformity_level-voxel": pAgF_z_vfwe_vals,
"logp_desc-uniformity_level-voxel": pAgF_logp_vfwe_vals,
"p_desc-uniformityMass_level-cluster": pAgF_p_cmfwe_vals,
"z_desc-uniformityMass_level-cluster": pAgF_z_cmfwe_vals,
"logp_desc-uniformityMass_level-cluster": pAgF_logp_cmfwe_vals,
"p_desc-uniformitySize_level-cluster": pAgF_p_csfwe_vals,
"z_desc-uniformitySize_level-cluster": pAgF_z_csfwe_vals,
"logp_desc-uniformitySize_level-cluster": pAgF_logp_csfwe_vals,
# association analysis
"p_desc-association_level-voxel": pFgA_p_vfwe_vals,
"z_desc-association_level-voxel": pFgA_z_vfwe_vals,
"logp_desc-association_level-voxel": pFgA_logp_vfwe_vals,
"p_desc-associationMass_level-cluster": pFgA_p_cmfwe_vals,
"z_desc-associationMass_level-cluster": pFgA_z_cmfwe_vals,
"logp_desc-associationMass_level-cluster": pFgA_logp_cmfwe_vals,
"p_desc-associationSize_level-cluster": pFgA_p_csfwe_vals,
"z_desc-associationSize_level-cluster": pFgA_z_csfwe_vals,
"logp_desc-associationSize_level-cluster": pFgA_logp_csfwe_vals,
}
description = ""
return maps, {}, description
def correct_fdr_indep(self, result, alpha=0.05):
"""Perform FDR correction using the Benjamini-Hochberg method.
Only call this method from within a Corrector.
.. versionchanged:: 0.0.12
Renamed from ``correct_fdr_bh`` to ``correct_fdr_indep``.
Parameters
----------
result : :obj:`~nimare.results.MetaResult`
Result object from a KDA meta-analysis.
alpha : :obj:`float`, optional
Alpha. Default is 0.05.
Returns
-------
maps : :obj:`dict`
Dictionary of 1D arrays corresponding to masked maps generated by
the correction procedure. The following arrays are generated by
this method: 'z_desc-uniformity_level-voxel' and 'z_desc-association_level-voxel'.
See Also
--------
nimare.correct.FDRCorrector : The Corrector from which to call this method.
Examples
--------
>>> meta = MKDAChi2()
>>> result = meta.fit(dset)
>>> corrector = FDRCorrector(method='indep', alpha=0.05)
>>> cresult = corrector.transform(result)