forked from ganzerkim/fMRI_Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuroimaging.py
165 lines (141 loc) · 6.12 KB
/
neuroimaging.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 27 09:17:07 2020
@author: MIT-DGMIF
"""
import os
import pandas as pd
import numpy as np
import scipy as sp
import random
import h5py
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
os.listdir('E:/Dataset/trends-assessment-prediction/')
features = pd.read_csv('E:/Dataset/trends-assessment-prediction/train_scores.csv')
loading = pd.read_csv('E:/Dataset/trends-assessment-prediction/loading.csv')
submission = pd.read_csv('E:/Dataset/trends-assessment-prediction/sample_submission.csv')
fnc = pd.read_csv("E:/Dataset/trends-assessment-prediction/fnc.csv")
reveal = pd.read_csv('E:/Dataset/trends-assessment-prediction/reveal_ID_site2.csv')
numbers = pd.read_csv('E:/Dataset/trends-assessment-prediction/ICN_numbers.csv')
fmri_mask = 'E:/Dataset/trends-assessment-prediction/fMRI_mask.nii'
import nilearn as nl
import nilearn.plotting as nlplt
import nibabel as nib
from nilearn import image
from nilearn import plotting
from nilearn import datasets
from nilearn import surface
smri = 'E:/Dataset/trends-assessment-prediction/ch2better.nii'
mask_img = nl.image.load_img(fmri_mask)
def load_subject(filename, mask_img):
subject_data = None
with h5py.File(filename, 'r') as f:
subject_data = f['SM_feature'][()]
# It's necessary to reorient the axes, since h5py flips axis order
subject_data = np.moveaxis(subject_data, [0,1,2,3], [3,2,1,0])
subject_img = nl.image.new_img_like(mask_img, subject_data, affine=mask_img.affine, copy_header=True)
return subject_img
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 3)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
nlplt.plot_prob_atlas(subject_img, bg_img=smri, view_type='filled_contours',
draw_cross=False, title='All %d spatial maps' % num_components, threshold='auto')
print("-" * 50)
#%%
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 3)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
#convert to 3d image
first_rsn = image.index_img(rsn, 0)
print(first_rsn.shape)
plotting.plot_stat_map(first_rsn)
print("-"*50)
#%%
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 1)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
#convert to 3d image
first_rsn = image.index_img(rsn, 0)
print(first_rsn.shape)
for img in image.iter_img(rsn):
# img is now an in-memory 3D img
plotting.plot_stat_map(img, threshold=3)
print("-"*50)
#%%
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 1)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
#convert to 3d image
first_rsn = image.index_img(rsn, 0)
print(first_rsn.shape)
plotting.plot_epi(first_rsn)
print("-"*50)
#%%
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 3)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
#convert to 3d image
first_rsn = image.index_img(rsn, 0)
print(first_rsn.shape)
plotting.plot_anat(first_rsn)
print("-"*50)
#%%
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 3)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
#convert to 3d image
first_rsn = image.index_img(rsn, 0)
print(first_rsn.shape)
plotting.plot_roi(first_rsn)
print("-"*50)
#%%
files = random.choices(os.listdir('E:/Dataset/trends-assessment-prediction/fMRI_train/'), k = 1)
for file in files:
subject = os.path.join('E:/Dataset/trends-assessment-prediction/fMRI_train/', file)
subject_img = load_subject(subject, mask_img)
print("Image shape is %s" % (str(subject_img.shape)))
num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
#convert to 3d image
first_rsn = image.index_img(rsn, 0)
print(first_rsn.shape)
plotting.plot_glass_brain(first_rsn,display_mode='lyrz')
print("-"*50)
#%%
motor_images = datasets.fetch_neurovault_motor_task()
stat_img = motor_images.images[0]
view = plotting.view_img_on_surf(first_rsn, threshold='90%')
view.open_in_browser()
view