-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathibma_lda_workflow.py
More file actions
293 lines (250 loc) · 9.05 KB
/
ibma_lda_workflow.py
File metadata and controls
293 lines (250 loc) · 9.05 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
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
import argparse
import itertools
import logging
import os
import os.path as op
import warnings
import nibabel as nib
from joblib import Parallel, delayed
from nimare.dataset import Dataset
from nimare.diagnostics import Jackknife
from nimare.meta.ibma import FixedEffectsHedges
from nimare.workflows import IBMAWorkflow
from ibma import AverageHedges
from outlier import remove_outliers
warnings.filterwarnings("ignore")
logging.basicConfig(level=logging.ERROR)
TASK_TO_TOPIC = {
"working_memory": "LDA100__78_memory_working_working memory",
"emotion_processing": "LDA100__12_emotional_emotion_response",
"reward_decision_making": "LDA100__15_reward_loss_monetary",
"motor": "LDA100__36_motor_primary_sensory",
"language": "LDA100__73_language_naming_linguistic",
"social_cognition": "LDA100__51_social_social cognition_cognition",
"response_inhibition": "LDA100__6_response_inhibition_response inhibition",
}
MANUAL_SELECTION = {
"working_memory": [
# 42805,
# 42807,
# 42803,
# 50291,
# 57499,
# 58192,
# 109901,
109902,
# 111344,
# 377201,
# 405163,
442126,
787480,
],
"social_cognition": [
19111,
19112,
19122,
19123,
381313,
],
"response_inhibition": [
28450,
43883,
29378,
51757,
57054,
550290,
191520,
191529,
392361,
],
"emotion_processing": [100438, 108833, 564310],
"reward_decision_making": [
16208,
550282,
],
"motor": [],
"language": [],
}
# EXCLUDE_COLLECTIONS = [457]
# EXCLUDE_COLLECTIONS = [457, 2621]
EXCLUDE_COLLECTIONS = [457, 2621, 7103, 7104]
ESTIMATORS = {
"mean": AverageHedges(method="mean", aggressive_mask=False),
"median": AverageHedges(method="median", aggressive_mask=False),
"trimmed_mean": AverageHedges(method="trimmed", aggressive_mask=False),
"winsorized_mean": AverageHedges(method="winsorized", aggressive_mask=False),
"fixed_effects": FixedEffectsHedges(aggressive_mask=False),
}
TARGET_IMGs = {
"working_memory": nib.load(
"/Users/julioaperaza/Documents/GitHub/large-scale-ibma/data/neurosynth/metamaps/working_memory.nii.gz"
),
"reward_decision_making": nib.load(
"/Users/julioaperaza/Documents/GitHub/large-scale-ibma/data/neurosynth/metamaps/reward.nii.gz"
),
"motor": nib.load(
"/Users/julioaperaza/Documents/GitHub/large-scale-ibma/data/neurosynth/metamaps/motor.nii.gz"
),
}
def _verbose_print(dset_task_sel):
metadata_sel_df = dset_task_sel.metadata
ids_sel = metadata_sel_df["id"].values
unique_cols = metadata_sel_df["collection_id"].unique()
n_collections = len(unique_cols)
print(f"\t{len(ids_sel)} images selected")
print(f"\t{n_collections} collections")
for col in unique_cols:
# Print collection id and images:
col_df = metadata_sel_df[metadata_sel_df["collection_id"] == col]
pmid_link = "https://pubmed.ncbi.nlm.nih.gov/"
coll_link = "https://neurovault.org/collections/"
image_link = "https://neurovault.org/images/"
print(col)
print(f"Link: {coll_link}{col}")
pmid_ = col_df["pmid"].unique()[0]
if pmid_ != "99999999":
print(f"Paper: {pmid_link}{pmid_}")
else:
print("Paper: None")
print("Images:")
for img_id in col_df["image_id"].values:
print(f"{image_link}{img_id}")
print("")
OUTLIER_METHODS = {
"heuristic": "None",
"heuristic-knn": "knn",
"heuristic-basic": "basic",
"heuristic-advanced": "advanced",
"heuristic-basic+advanced": "full",
}
def _get_parser():
parser = argparse.ArgumentParser(description="Run IBMA workflow")
parser.add_argument(
"--project_dir",
dest="project_dir",
required=True,
help="Path to project directory",
)
parser.add_argument(
"--n_cores",
dest="n_cores",
default=-1,
required=False,
help="CPUs",
)
return parser
def _select_dset(dset, mode, task):
metadata_df = dset.metadata
if mode == "all":
return dset
elif mode.startswith("heuristic"):
# Remove images without pmid
metadata_task_pmid_df = metadata_df[metadata_df["pmid"] != "99999999"]
dset_task_temp = dset.slice(metadata_task_pmid_df["id"].values)
# Skip this for now
# dset_task_temp = dset.copy()
method = OUTLIER_METHODS[mode]
target = TARGET_IMGs[task] if method in ["basic", "full"] else None
# Exclude outliers and non-stat maps
dset_task_temp = remove_outliers(
dset_task_temp,
method=method,
target=target,
)
ids_sel = dset_task_temp.metadata["id"].values
elif mode == "manual":
img_ids = MANUAL_SELECTION[task]
metadata_sel_df = metadata_df[metadata_df["image_id"].isin(img_ids)]
ids_sel = metadata_sel_df["id"].values
else:
raise ValueError("Invalid mode")
return dset.slice(ids_sel)
def run_ibma(estimator, dset, result_fn, n_cores=1):
# Set voxel_thresh to a high value to skip diagnostics for now
diagnostics = Jackknife(voxel_thresh=10000, n_cores=n_cores)
workflow = IBMAWorkflow(
estimator=estimator,
diagnostics=diagnostics,
n_cores=n_cores,
)
result = workflow.fit(dset)
result.save(result_fn)
print("\t\t\tDone!")
def run_ibma_perm(i, dset, n_images, out_dir, estimators, n_cores=1):
print(f"\tRunning IBMA, with {n_images} random images, permutation {i}")
metadata_df = dset.metadata
metadata_rand_df = metadata_df.sample(n=n_images, replace=False, random_state=i)
dset_rand = dset.slice(metadata_rand_df["id"].values)
for label, estimator in estimators.items():
print("\t\tUsing estimator: ", label)
result_fn = op.join(out_dir, f"{label}_perm-{i:02d}_result.pkl.gz")
run_ibma(estimator, dset_rand, result_fn, n_cores=n_cores)
def main(project_dir, n_perm=100, perm=False, verbose=0, n_cores=-1):
project_dir = op.abspath(project_dir)
data_dir = op.join(project_dir, "data")
image_dir = op.join(data_dir, "neurovault", "images")
results_dir = op.join(project_dir, "results", "ibma-lda")
n_perm = int(n_perm)
n_cores = int(n_cores)
dset = Dataset.load(op.join(data_dir, "neurovault_all_lda_dataset.pkl"))
dset.update_path(image_dir)
modes = [
# "all",
"heuristic",
# "heuristic-knn",
# "heuristic-basic",
# "heuristic-advanced",
# "heuristic-basic+advanced",
# "manual",
]
tasks = [
# "working_memory",
# "social_cognition",
# "response_inhibition",
# "reward_decision_making",
"motor",
]
for task, mode in itertools.product(tasks, modes):
print(f"Running IBMA for task and mode: {task}, {mode}")
# Define output directories
ibma_dir = op.join(results_dir, task, mode)
os.makedirs(ibma_dir, exist_ok=True)
# Select images associated with the task
topic_id = TASK_TO_TOPIC[task]
topic_mask = dset.annotations[topic_id] > 0.05
task_ids = dset.annotations[topic_mask].id.values
print(f"\t{len(task_ids)} images associated with topic {task}")
dset_task = dset.slice(task_ids)
metadata_task_df = dset_task.metadata
# Remove HCP and other known outliers images
non_hcp_df = metadata_task_df[~metadata_task_df["collection_id"].isin(EXCLUDE_COLLECTIONS)]
dset_task = dset_task.slice(non_hcp_df["id"].values)
dset_task_sel = _select_dset(dset_task, mode, task)
dset_task_sel.metadata.to_csv(op.join(ibma_dir, f"metadata_{mode}.csv"), index=False)
# if verbose > 0:
if mode == "heuristic":
_verbose_print(dset_task_sel)
# Run IBMA
unique_cols = dset_task_sel.metadata["collection_id"].unique()
print(f"\tRunning IBMA, with {len(dset_task_sel.images)} images, {len(unique_cols)} cols")
for label, estimator in ESTIMATORS.items():
print("\t\tUsing estimator: ", label)
result_fn = op.join(ibma_dir, f"{label}_result.pkl.gz")
run_ibma(estimator, dset_task_sel, result_fn, n_cores=n_cores)
# Run IBMA on 100 samples of n_sel_images randomly sampled images
if mode.startswith("heuristic") and perm:
n_sel_images = len(dset_task_sel.images)
ibma_perm_dir = op.join(ibma_dir, "permutation")
os.makedirs(ibma_perm_dir, exist_ok=True)
print(f"\tRunning IBMA, with {n_perm} random samples of {n_sel_images} images")
print("Using ", n_cores, " cores")
Parallel(n_jobs=n_cores)(
delayed(run_ibma_perm)(i, dset_task, n_sel_images, ibma_perm_dir, ESTIMATORS)
for i in range(n_perm)
)
def _main(argv=None):
option = _get_parser().parse_args(argv)
kwargs = vars(option)
main(**kwargs)
if __name__ == "__main__":
_main()