-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepping_data.py
103 lines (79 loc) · 2.92 KB
/
prepping_data.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
import numpy as np
import pandas as pd
import nibabel as nib
from nilearn.masking import apply_mask
from nilearn.input_data import NiftiMasker
from nilearn.image import resample_img
from sklearn.preprocessing import FunctionTransformer
def hdr_to_Nifti(files):
"""
Convert hdr files to Nifti-like objects
Parameters
----------
files: list of paths to each hdr file
Returns
----------
array: array of Nifti-like objects
"""
array = []
for element in files:
array = np.append(array, nib.load(element))
print('array size: ', array.shape, '\narray type: ', type(array))
return array
def y_transformer(y, func = np.log1p):
"""
Transform y using a specified function
Parameters
----------
y: variable to transform
func: list of numpy transformations to apply to the variable
Returns
----------
df_y: DataFrame containing y and the transformed y according to the specified transformations
"""
df_y = pd.DataFrame(y.tolist(), columns = ["y"])
for element in func:
transformer = FunctionTransformer(element, validate=True)
Y_transformed = transformer.fit_transform(y.reshape(-1,1))
Y_transformed = Y_transformed[:,0]
df_y[str(element).replace("<ufunc '", ""). replace("'>","")] = Y_transformed.tolist()
return df_y
def extract_signal(data, mask="template", standardize = True):
"""
Apply a mask to extract the signal from the data and save the mask
in a html format
Parameters
----------
data: list of Niimg-like objects
mask: strategy to compute the mask. By default the gray matter is extracted based on the MNI152 brain mask
standardize: strategy to standardize the signal. The signal is z-scored by default
Returns
----------
masker_all: mask of the data
masker_gm: array containing the extracted signal
See also NifitMasker documentation
"""
masker_all = NiftiMasker(mask_strategy = mask,standardize=standardize, verbose = 1, reports = True)
masker_gm = masker_all.fit_transform(data)
print("mean: ", round(masker_gm.mean(),2), "\nstd: ", round(masker_gm.std(),2))
print("Signal size: ", masker_gm.shape)
report = masker_all.generate_report()
report.save_as_html("masker_report.html")
return masker_all, masker_gm
def extract_signal_from_mask(data, mask):
"""
Apply a pre-computed mask to extract the signal from the data
Parameters
----------
data: Niimg-like object
mask: mask to apply to the data
Returns
----------
signal: extracted signal from mask
See also nilearn masking documentation
"""
affine = data[0].affine
resample_mask = resample_img(mask,affine)
signal = apply_mask(data, resample_mask, ensure_finite=True)
print(signal.shape, type(signal))
return signal