-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathibma.py
More file actions
165 lines (130 loc) · 6.21 KB
/
ibma.py
File metadata and controls
165 lines (130 loc) · 6.21 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
import logging
import math
import numpy as np
from nilearn.input_data import NiftiMasker
from nimare.meta.ibma import IBMAEstimator
from nimare.transforms import d_to_g, t_to_d, z_to_p, z_to_t
from nimare.utils import _boolean_unmask
from scipy.stats import norm
LGR = logging.getLogger(__name__)
def calculate_means(estimates, n_maps, gamma=0.3, method="mean"):
K = estimates.shape[0] # K samples, V voxels
if method == "mean":
est_maps = estimates.mean(axis=0)
mean_absolute_deviation = np.abs(estimates - est_maps).mean(axis=0)
var_maps = mean_absolute_deviation**2
var_mean_maps = (1 / n_maps[0, :]) * var_maps
return est_maps, var_mean_maps
elif method == "median":
est_maps = np.median(estimates, axis=0)
median_absolute_deviation = np.median(np.abs(estimates - est_maps), axis=0) * (
1 / norm.ppf(3 / 4)
)
var_maps = median_absolute_deviation**2
var_mean_maps = (math.pi / (2 * n_maps[0, :])) * var_maps
return est_maps, var_mean_maps
elif (method == "trimmed") or (method == "winsorized"):
K_gamma = int(gamma * K / 2)
# print(K, K_gamma)
# Sort the estimates along each voxel
estimates_sorted = np.sort(estimates, axis=0)
# Trimmed mean calculation
if K_gamma == 0:
estimates_trimmed = estimates_sorted # Use all values
else:
estimates_trimmed = estimates_sorted[K_gamma:-K_gamma, :]
trimmed_mean = np.mean(estimates_trimmed, axis=0)
# Windsorized mean calculation
estimates_winsorized = estimates_sorted.copy()
if K_gamma > 0: # Only modify the array if K_gamma is greater than 0
estimates_winsorized[:K_gamma, :] = estimates_sorted[K_gamma, :]
estimates_winsorized[-K_gamma:, :] = estimates_sorted[-K_gamma - 1, :]
winsorized_mean = np.mean(estimates_winsorized, axis=0)
# Windsorized estimate of data variance
winsorized_var = (
K_gamma * (estimates_sorted[K_gamma, :] - winsorized_mean) ** 2
+ np.sum((estimates_winsorized[K_gamma:-K_gamma, :] - winsorized_mean) ** 2, axis=0)
+ K_gamma * (estimates_sorted[-K_gamma - 1, :] - winsorized_mean) ** 2
) / (K - 1)
if method == "trimmed":
# Calculate the variance of the mean
var_mean_maps = (1 / (n_maps[0, :] - 2 * K_gamma)) * winsorized_var
return trimmed_mean, var_mean_maps
elif method == "winsorized":
# Calculate the variance of the mean
var_mean_maps = (1 / n_maps[0, :]) * winsorized_var
return winsorized_mean, var_mean_maps
else:
raise ValueError(f"Method {method} not recognized.")
class AverageHedges(IBMAEstimator):
_required_inputs = {"t_maps": ("image", "t"), "sample_sizes": ("metadata", "sample_sizes")}
def __init__(self, gamma=0.2, method="mean", **kwargs):
super().__init__(**kwargs)
self.gamma = gamma
self.method = method
def _generate_description(self):
description = (
f"An image-based meta-analysis was performed with NiMARE"
"(RRID:SCR_017398; \\citealt{Salo2023}), on "
f"{len(self.inputs_['id'])} t-statistic images using Heges' g as point estimates "
"and the variance of bias-corrected Cohen's in a Weighted Least Squares approach "
"\\citep{brockwell2001comparison,bossier2019}, "
f"with an a priori tau-squared value of defined across all voxels."
)
return description
def _fit_model(self, t_maps, study_mask=None):
"""Fit the model to the data."""
n_studies, n_voxels = t_maps.shape
if study_mask is None:
# If no mask is provided, assume all studies are included. This is always the case
# when using the aggressive mask.
study_mask = np.arange(n_studies)
sample_sizes = np.array([np.mean(self.inputs_["sample_sizes"][idx]) for idx in study_mask])
n_maps = np.tile(sample_sizes, (n_voxels, 1)).T
# Calculate Hedge's g maps: Standardized mean
cohens_maps = t_to_d(t_maps, n_maps)
hedges_maps = d_to_g(cohens_maps, n_maps)
del sample_sizes, cohens_maps
est_maps, var_mean_maps = calculate_means(
hedges_maps,
n_maps,
gamma=self.gamma,
method=self.method,
)
z_map = est_maps / np.sqrt(var_mean_maps)
p_map = z_to_p(z_map)
dof = est_maps.shape[0] - 1
t_map = z_to_t(z_map, dof)
cohens_maps = t_to_d(t_map, dof)
new_hedges_maps = d_to_g(cohens_maps, dof)
return z_map, p_map, est_maps, new_hedges_maps # , hedges_maps
def _fit(self, dataset):
self.dataset = dataset
self.masker = self.masker or dataset.masker
if not isinstance(self.masker, NiftiMasker):
LGR.warning(
f"A {type(self.masker)} mask has been detected. "
"Masks which average across voxels will likely produce biased results when used "
"with this Estimator."
)
# input_maps = np.zeros_like(self.inputs_["t_maps"])
if self.aggressive_mask:
voxel_mask = self.inputs_["aggressive_mask"]
result_maps = self._fit_model(self.inputs_["t_maps"][:, voxel_mask])
z_map, p_map, est_map, es_map = tuple(
map(lambda x: _boolean_unmask(x, voxel_mask), result_maps)
)
else:
n_voxels = self.inputs_["t_maps"].shape[1]
z_map, p_map, est_map, es_map = [np.zeros(n_voxels, dtype=float) for _ in range(4)]
for bag in self.inputs_["data_bags"]["t_maps"]:
(
z_map[bag["voxel_mask"]],
p_map[bag["voxel_mask"]],
est_map[bag["voxel_mask"]],
es_map[bag["voxel_mask"]],
# input_maps[bag["study_mask"], bag["voxel_mask"]],
) = self._fit_model(bag["values"], bag["study_mask"])
maps = {"z": z_map, "p": p_map, "est": est_map, "es": es_map} # "input": input_maps
description = self._generate_description()
return maps, {}, description