-
Notifications
You must be signed in to change notification settings - Fork 10
/
MetaAnalysis.py
326 lines (273 loc) · 10.5 KB
/
MetaAnalysis.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
#!/usr/bin/env python
# coding: utf-8
"""
Run ALE meta-analysis based on code from JB Poline
"""
import argparse
import pickle
import pandas
import nibabel
import nimare
import os
import nilearn.image
import nilearn.input_data
import nilearn.plotting
import nilearn.masking
import matplotlib.pyplot as plt
from statsmodels.stats.multitest import multipletests
from narps import Narps, hypnums, hypotheses
from narps import NarpsDirs # noqa, flake8 issue
from ALEextract import get_activations, get_sub_dict
# use binarized/thresholded maps and z maps to create
# thresholded z maps
def get_thresholded_Z_maps(narps, verbose=False, overwrite=False):
for teamID in narps.complete_image_sets['thresh']:
if 'zstat' not in narps.teams[teamID].images['thresh']:
narps.teams[teamID].images['thresh']['zstat'] = {}
for hyp in range(1, 10):
if hyp not in narps.teams[teamID].images['unthresh']['zstat']:
# fill missing data with nan
if verbose:
print('no unthresh zstat present for', teamID, hyp)
continue
if hyp not in narps.teams[teamID].images['thresh']['resampled']:
# fill missing data with nan
if verbose:
print('no thresh resampled present for', teamID, hyp)
continue
zfile = narps.teams[teamID].images['unthresh']['zstat'][hyp]
if not os.path.exists(zfile):
if verbose:
print('no image present:', zfile)
continue
threshfile = narps.teams[teamID].images['thresh']['resampled'][hyp]
if not os.path.exists(threshfile):
if verbose:
print('no image present:', threshfile)
continue
outfile = zfile.replace('unthresh', 'thresh')
assert outfile != zfile
narps.teams[teamID].images['thresh']['zstat'][hyp] = outfile
if not os.path.exists(outfile) or overwrite:
if verbose:
print('creating thresholded Z for', teamID, hyp)
unthresh_img = nibabel.load(zfile)
unthresh_data = unthresh_img.get_data()
thresh_img = nibabel.load(threshfile)
thresh_data = thresh_img.get_data()
thresh_z_data = thresh_data * unthresh_data
thresh_z_img = nibabel.Nifti1Image(
thresh_z_data,
affine=thresh_img.affine)
thresh_z_img.to_filename(outfile)
else:
if verbose:
print('using existing thresholded Z for', teamID, hyp)
return(narps)
def extract_peak_coordinates(narps, hyp,
overwrite=False, threshold=0.,
verbose=False):
'''
def extract(dir_path, filename, threshold=0., load=True, save_dir=None):
# threshold_type='percentile',
Extracts coordinates from the data and put it in a
dictionnary using Nimare structure.
Inputs
-----------------
dir_path : str
Path to the folder containing the studies folders
filename : str
Name of the image to look for inside each study folder
threshold : float
Threshold (in z scale) zero values below threshold in input images
# used to be: between 0-1 percentile
load : bool
If True, try to load a previously dumped dict if any.
If dump fails or False, compute a new one.
Returns:
-----------------
(dict): Dictionnary storing the coordinates using the Nimare
structure.
'''
gray_mask = nibabel.load(narps.dirs.MNI_mask) # no masking
dictfile = os.path.join(
narps.dirs.dirs['ALE'],
'hyp%d_peak_coords.pkl' % hyp
)
# Loading previously computed dict if any
if os.path.exists(dictfile) and not overwrite:
with open(dictfile, 'rb') as f:
ds_dict = pickle.load(f)
return(ds_dict)
res = []
# get activations for all teams with a thresh zstat
for teamID in narps.complete_image_sets['thresh']:
if 'zstat' not in narps.teams[teamID].images['thresh']:
continue
if hyp not in narps.teams[teamID].images['thresh']['zstat']:
continue
thresh_zfile = narps.teams[teamID].images['thresh']['zstat'][hyp]
if verbose:
print('get_activations:', thresh_zfile, threshold)
XYZ = get_activations(thresh_zfile, gray_mask, threshold)
if XYZ is not None:
res.append(get_sub_dict(XYZ[0], XYZ[1], XYZ[2]))
else:
res.append({})
# Removing potential None values
res = list(filter(None, res))
# Merging all dictionaries
ds_dict = {k: v for k, v in enumerate(res)}
# Dumping
with open(dictfile, 'wb') as f:
pickle.dump(ds_dict, f)
return(ds_dict)
def run_ALE(ds_dict, hyp, narps, overwrite=False):
outfile = os.path.join(
narps.dirs.dirs['ALE'],
'ALE_results_hyp%d.pkl' % hyp
)
if os.path.exists(outfile) and not overwrite:
print('using saved meta-analysis results')
with open(outfile, 'rb') as f:
res = pickle.load(f)
return(res)
ds = nimare.dataset.Dataset(ds_dict)
ALE = nimare.meta.cbma.ale.ALE()
res = ALE.fit(ds)
with open(outfile, 'wb') as f:
pickle.dump(res, f)
return(res)
def save_results(hyp, res, narps, fdr_thresh=0.05):
# saving results images
images = {}
for i in ['ale', 'p', 'z']:
images[i] = res.get_map(i)
save_file_img = os.path.join(
narps.dirs.dirs['ALE'],
'img_%s_hyp%d.nii.gz' % (i, hyp))
nibabel.save(images[i], save_file_img)
# -------------------------- threshold with fdr --------------- #
masker = nilearn.input_data.NiftiMasker(
mask_img=narps.dirs.MNI_mask
)
pvals = masker.fit_transform(images['p'])
fdr_results = multipletests(pvals[0, :], 0.05, 'fdr_tsbh')
images['fdr_oneminusp'] = masker.inverse_transform(1 - fdr_results[1])
images['fdr_oneminusp'].to_filename(os.path.join(
narps.dirs.dirs['ALE'],
'hypo%d_fdr_oneminusp.nii.gz' % hyp))
images['fdr_thresholded'] = masker.inverse_transform(
(fdr_results[1] < fdr_thresh).astype('int'))
images['fdr_thresholded'].to_filename(os.path.join(
narps.dirs.dirs['ALE'],
'hypo%d_fdr_thresholded.nii.gz' % hyp))
return(images)
def make_figures(narps, hyp, images, fdr_thresh=0.05):
cut_coords = [-24, -10, 4, 18, 32, 52, 64]
for i in ['ale', 'p', 'z', 'fdr_oneminusp']:
nilearn.plotting.plot_stat_map(images[i], title=i)
outfile = os.path.join(
narps.dirs.dirs['figures'],
'hyp%d_ALE_%s.png' % (hyp, i)
)
plt.savefig(outfile, bbox_inches='tight')
plt.close()
nilearn.plotting.plot_stat_map(
images['fdr_oneminusp'],
threshold=1 - fdr_thresh,
title='H%d meta-analysis (FDR < %0.2f' % (hyp, fdr_thresh),
display_mode="z",
colorbar=True,
cmap='jet',
cut_coords=cut_coords,
annotate=False)
outfile = os.path.join(
narps.dirs.dirs['figures'],
'hyp%d_ALE_fdr_thresh_%0.2f.png' % (hyp, fdr_thresh)
)
plt.savefig(outfile, bbox_inches='tight')
plt.close()
return(None)
def make_combined_figure(narps, thresh=0.95):
fig, ax = plt.subplots(7, 1, figsize=(12, 24))
cut_coords = [-24, -10, 4, 18, 32, 52, 64]
for i, hyp in enumerate(hypnums):
pmap = os.path.join(
narps.dirs.dirs['ALE'],
'hypo%d_fdr_thresholded.nii.gz' % hyp)
tmap = os.path.join(
narps.dirs.dirs['ALE'],
'img_z_hyp%d.nii.gz' % hyp)
pimg = nibabel.load(pmap)
timg = nibabel.load(tmap)
pdata = pimg.get_fdata()
tdata = timg.get_fdata()
threshdata = (pdata > thresh)*tdata
threshimg = nibabel.Nifti1Image(threshdata, affine=timg.affine)
nilearn.plotting.plot_stat_map(
threshimg,
threshold=0.1,
display_mode="z",
colorbar=True,
title='hyp %d:' % hyp+hypotheses[hyp],
vmax=8,
cmap='jet',
cut_coords=cut_coords,
axes=ax[i])
plt.savefig(os.path.join(
narps.dirs.dirs['figures'],
'ALE_map.png'), bbox_inches='tight')
plt.close(fig)
if __name__ == "__main__":
# parse arguments
parser = argparse.ArgumentParser(
description='Run ALE meta-analysis')
parser.add_argument('-b', '--basedir',
help='base directory')
parser.add_argument('-t', '--test',
action='store_true',
help='use testing mode (no processing)')
parser.add_argument('-v', '--verbose',
action='store_true',
help='verbose mode')
parser.add_argument('-o', '--overwrite',
action='store_true',
help='overwrite existing results')
args = parser.parse_args()
# set up base directory
if args.basedir is not None:
basedir = args.basedir
elif 'NARPS_BASEDIR' in os.environ:
basedir = os.environ['NARPS_BASEDIR']
print("using basedir specified in NARPS_BASEDIR")
else:
basedir = '/data'
print("using default basedir:", basedir)
# setup main class
narps = Narps(basedir)
narps.load_data()
output_dir = narps.dirs.get_output_dir('ALE')
# Load full metadata and put into narps structure
narps.metadata = pandas.read_csv(
os.path.join(narps.dirs.dirs['metadata'], 'all_metadata.csv'))
if not args.test:
# create thresholded versions of Z maps
narps = get_thresholded_Z_maps(
narps,
verbose=args.verbose,
overwrite=args.overwrite)
# extract peak coordinates
for hyp in range(1, 10):
print('Hypothesis', hyp)
ds_dict = extract_peak_coordinates(
narps,
hyp,
overwrite=args.overwrite,
verbose=args.verbose)
# Performing ALE
res = run_ALE(ds_dict, hyp, narps, overwrite=args.overwrite)
images = save_results(hyp, res, narps)
make_figures(narps, hyp, images)
# make a figure with all hypotheses
make_combined_figure(narps)