-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract.py
307 lines (238 loc) · 9.66 KB
/
extract.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
"""Contain function for extracting coordinates from folders."""
import os
import shutil
import numpy as np
import nilearn
from nilearn import masking, datasets, image
from nipy.labs.statistical_mapping import get_3d_peaks
import multiprocessing
from joblib import Parallel, delayed
import ntpath
import nibabel as nib
import re
import scipy
from tools import pickle_dump, pickle_load
save_dir = 'save/'
template = datasets.load_mni152_template()
gray_mask = masking.compute_gray_matter_mask(template)
def get_sub_dict(XYZ, path_dict):
"""
Build sub dictionnary of a study using the nimare structure.
Args:
XYZ (tuple): Size 3 tuple of list storing the X Y Z coordinates.
path_dict (dict): Dict which has map name ('t', 'z', 'con', 'se')
as keys and absolute path to the image as values.
Returns:
(dict): Dictionary storing the coordinates for a
single study using the Nimare structure.
"""
d = {
'contrasts': {
'0': {
'metadata': {'sample_sizes': 119}
}
}
}
if XYZ is not None:
d['contrasts']['0']['coords'] = {
'x': XYZ[0],
'y': XYZ[1],
'z': XYZ[2],
'space': 'MNI'
}
d['contrasts']['0']['sample_sizes'] = 119
if path_dict is not None:
d['contrasts']['0']['images'] = path_dict
return d
def get_activations(path, threshold):
"""
Retrieve the xyz activation coordinates from an image.
Args:
path (string or Nifti1Image): Path to or object of a
nibabel.Nifti1Image from which to extract coordinates.
threshold (float): Same as the extract_from_paths function.
Returns:
(tuple): Size 3 tuple of lists storing respectively the X, Y and
Z coordinates
"""
X, Y, Z = [], [], []
try:
img = nilearn.image.load_img(path)
except ValueError: # File path not found
print(f'File {path} not found. Ignored.')
return None
if np.isnan(img.get_fdata()).any():
print(f'Img {path} contains Nan. Ignored.')
return None
img = image.resample_to_img(img, template)
peaks = get_3d_peaks(img, mask=gray_mask, threshold=threshold)
if not peaks:
return X, Y, Z
for peak in peaks:
X.append(peak['pos'][0])
Y.append(peak['pos'][1])
Z.append(peak['pos'][2])
del peaks
return X, Y, Z
def extract_from_paths(path_dict, data=['coord', 'path'],
threshold=1.96, tag=None, load=True):
"""
Extract data from given images.
Extracts data (coordinates, paths...) from the data and put it in a
dictionnary using Nimare structure.
Args:
path_dict (dict): Dict which keys are study names and values
are another dict which keys are map names ('z', 'con', 'se', 't')
and values are absolute paths (string) to these maps.
data (list): Data to extract. 'coord' and 'path' available.
threshold (float): value below threshold are ignored. Used for
peak detection.
tag (str): Name of the file to load/dump.
load (bool): If True, load a potential existing result.
If False or not found, compute again.
Returns:
(dict): Dictionnary storing the coordinates using the Nimare
structure.
"""
if tag is not None:
# Loading previously computed dict if any
ds_dict = pickle_load(save_dir+tag, load=load)
if ds_dict is not None:
return ds_dict
# Computing a new dataset dictionary
def extract_pool(name, map_dict):
"""Extract activation for multiprocessing."""
print(f'Extracting {name}...')
XYZ = None
if 'coord' in data:
XYZ = get_activations(map_dict['z'], threshold)
if XYZ is None:
return
if 'path' in data:
# base, filename = ntpath.split(path)
# file, ext = filename.split('.', 1)
# path_dict = {'z': path}
# for map_type in ['t', 'con', 'se']:
# file_path = f'{base}/{file}_{map_type}.{ext}'
# if os.path.isfile(file_path):
# path_dict[map_type] = file_path
# return get_sub_dict(XYZ, path_dict)
return get_sub_dict(XYZ, map_dict)
if XYZ is not None:
return get_sub_dict(XYZ, None)
return
n_jobs = multiprocessing.cpu_count()
res = Parallel(n_jobs=n_jobs, backend='threading')(
delayed(extract_pool)(name, maps) for name, maps in path_dict.items())
# Removing potential None values
res = list(filter(None, res))
# Merging all dictionaries
ds_dict = {k: v for k, v in enumerate(res)}
if tag is not None:
pickle_dump(ds_dict, save_dir+tag) # Dumping
return ds_dict
# def process(Path, suffix='_resampled'):
# """
# Process images to resample them into MNI template.
# Args:
# Path (list): List of paths (string) of images.
# suffix (string): Suffix added to the original file. Note that the
# output file is stored in the same dir as the input one.
# """
# for path in Path:
# try:
# img = nilearn.image.load_img(path)
# except ValueError: # File path not found
# print(f'File {path} not found. Ignored.')
# continue
# if np.isnan(img.get_fdata()).any():
# print(f'File {path} contains Nan. Ignored.')
# continue
# img = image.resample_to_img(img, template)
# var = nib.Nifti1Image(img.get_fdata(), img.affine)
# base, filename = ntpath.split(path)
# file, ext = filename.split('.', 1)
# print(f'Resampling {path}...')
# img.to_filename(f'{base}/{file}{suffix}.{ext}')
# var.to_filename(f'{base}/{file}_var.{ext}')
def process(studies, o_dir, n_sub, s1, s2, rmdir=False,
ignore_if_exist=False, random_state=None):
"""
Process data by simulating subjects from studies' avg contrasts.
Args:
studies (dict): Dict with studies' alphanum names as keys and
absolute path to studies' folder as values.
o_dir (string): Path to output directory in which to store
processed data.
n_sub (int): Number of subjects to simulate.
s1 (float): Standard deviation of the gaussian noise.
s2 (float): Standard deviation of the gaussian kernel.
rmdir (bool, optional): Whether to delete existing output directory
before writing process data.
ignore_if_exist (bool, optional): Whether to ignore this process
if the output directory exists.
"""
np.random.seed(random_state)
if ignore_if_exist and os.path.exists(o_dir):
print(f'Dir {o_dir} exists. Process ignored.')
return
if rmdir and os.path.exists(o_dir):
shutil.rmtree(o_dir)
o_dir = os.path.join(o_dir, '') # Adds trailing slash if not already
# for study_name, path in studies.items():
def process_pool(study_name, path):
print(f'Processing {study_name}...')
try:
z_img = nilearn.image.load_img(path)
except ValueError: # File path not found
print(f'File {path} not found. Ignored.')
return
if np.isnan(z_img.get_fdata()).any():
print(f'Image {path} contains Nan. Ignored.')
return
if not re.match(r'^\w+$', study_name):
raise ValueError('Study {study_name} contains invalid caracters.')
o_study_path = f'{o_dir}{study_name}/'
os.makedirs(o_study_path, exist_ok=True)
_, filename = ntpath.split(path)
file, ext = filename.split('.', 1)
z_img = image.resample_to_img(z_img, template)
z_img.to_filename(f'{o_study_path}{file}.{ext}')
sub_imgs = []
for i in range(1, n_sub+1):
print(f'Simulating subject {i}...', end='\r')
sub_img = sim_sub(z_img, s1, s2)
# sub_dir = f'{o_study_path}sub-{str(i).zfill(len(str(n_sub)))}/'
# os.makedirs(sub_dir, exist_ok=True)
# sub_img.to_filename(f'{sub_dir}{filename}')
sub_imgs.append(sub_img)
std_img = nilearn.image.math_img('np.std(imgs, axis=3)', imgs=sub_imgs)
del sub_imgs
std_img.to_filename(f'{o_study_path}{file}_se.{ext}')
con_img = nilearn.image.math_img('np.multiply(img1, img2)',
img1=z_img, img2=std_img)
del z_img, std_img
con_img.to_filename(f'{o_study_path}{file}_con.{ext}')
del con_img
n_jobs = multiprocessing.cpu_count()//2
# print([(name, paths['z']) for name, paths in studies.items()])
Parallel(n_jobs=n_jobs, backend='threading')(delayed(process_pool)
(name, paths['z']) for name, paths in studies.items())
def sim_sub(img, s1, s2):
"""
Simulate a subject's contrast from an average map.
Generate random gaussian noise of the shape of the given image, convolve
this noise with a gaussian kernel and add the result to the given image.
Args:
img (nibabel.Nifti1Image): Base image.
s1 (float): Standard deviation of the gaussian noise.
s2 (float): Standard deviation of the gaussian kernel applied to the
generated noise.
Returns:
(nibabel.Nifti1Image): Noised image.
"""
data = img.get_fdata()
noise = np.random.normal(scale=s1, size=data.shape)
noise = scipy.ndimage.gaussian_filter(noise, s2)
noise = np.ma.masked_array(noise, np.logical_not(gray_mask.get_fdata()))
return nib.Nifti1Image(data+noise, img.affine)