-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
171 lines (139 loc) · 5.7 KB
/
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
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
import csv
import os
import matplotlib.pyplot as plt
import numpy as np
from dipy.core.gradients import gradient_table
from dipy.io import read_bvals_bvecs
from dipy.io.image import load_nifti, save_nifti
from dipy.reconst import dti
from dipy.segment.mask import median_otsu
from utils import multi_slice_viewer
def vis_all(data):
for i in range(data.shape[-1]):
plt.imshow(data[:, :, i].T, cmap='gray', origin='lower')
plt.show()
def visualize(data):
# visualize middle slice
axial_middle = data.shape[2] // 2
plt.figure('Showing the datasets')
plt.subplot(1, 2, 1).set_axis_off()
# without diffusion weighting
plt.imshow(data[:, :, axial_middle, 0].T, cmap='gray', origin='lower')
plt.subplot(1, 2, 2).set_axis_off()
# with dw
plt.imshow(data[:, :, axial_middle, 10].T, cmap='gray', origin='lower')
plt.show()
# plt.savefig('data.png', bbox_inches='tight')
def visualize3D(FA):
# plt.figure('FA')
# ax = plt.axes(projection="3d")
# plt.show()
multi_slice_viewer(FA)
def traverseFA(data, fname):
for i in range(data.shape[2]):
# plt.imshow(data[:,:,mid,0].T,cmap='gray',origin='lower')
plt.imsave(fname + str(i) + ".png", data[:, :, i].T, cmap='gray', origin='lower')
def get25DSlice(data, fname, dir=3):
mid0 = data.shape[0] // 2 - 8
mid1 = data.shape[1] // 2 - 8
mid2 = data.shape[2] // 2 - 8
# i = 15
# plt.imsave(fname + "mid0_"+str(i)+".png", data[mid0, :, :].T, cmap='gray', origin='lower')
# if dir==3:
# plt.imsave(fname + "mid1_"+str(i)+".png", data[:, mid1, :].T, cmap='gray', origin='lower')
# plt.imsave(fname + "mid2_"+str(i)+".png", data[:, :, mid2].T, cmap='gray', origin='lower')
for i in range(16):
plt.imsave(fname + "mid0_" + str(i) + ".png", data[mid0 + i, :, :].T, cmap='gray', origin='lower')
if dir == 3:
plt.imsave(fname + "mid1_" + str(i) + ".png", data[:, mid1 + i, :].T, cmap='gray', origin='lower')
plt.imsave(fname + "mid2_" + str(i) + ".png", data[:, :, mid2 + i].T, cmap='gray', origin='lower')
def raw2FA(source, file, des):
if os.path.exists(des + file[:-5] + "_fa.nii.gz"):
print(file[:-5], "already processed")
return
fbval = source + file
fdwi = source + file[:-5] + ".nii.gz"
fbvec = source + file[:-5] + ".bvec"
data, affine = load_nifti(fdwi)
print(fdwi, data.shape)
bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
gtab = gradient_table(bvals, bvecs)
maskdata, mask = median_otsu(data, vol_idx=range(10, 50), median_radius=3, numpass=1, autocrop=True,
dilate=2)
tenmodel = dti.TensorModel(gtab)
tenfit = tenmodel.fit(maskdata)
FA = dti.fractional_anisotropy(tenfit.evals)
FA[np.isnan(FA)] = 0
save_nifti(des + file[:-5] + "_fa.nii.gz", FA.astype(np.float32), affine)
# visualize(data)
def raw2FA_new(fdwi, fbval=None, fbvec=None):
data, affine = load_nifti(fdwi)
print(fdwi, data.shape)
bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
gtab = gradient_table(bvals, bvecs)
maskdata, mask = median_otsu(data, vol_idx=range(10, 20), median_radius=3, numpass=1, autocrop=True,
dilate=2)
tenmodel = dti.TensorModel(gtab)
tenfit = tenmodel.fit(maskdata)
FA = dti.fractional_anisotropy(tenfit.evals)
FA[np.isnan(FA)] = 0
plt.imshow(FA[:, :, FA.shape[2] // 2].T, cmap='gray', origin='lower')
plt.show()
pass
# save_nifti(des + file[:-5] + "_fa.nii.gz", FA.astype(np.float32), affine)
def FA2Slice(source, file, des, dir=3):
if not os.path.exists(des):
os.mkdir(des)
FA, affine = load_nifti(source + file)
print(FA.shape)
# visualize3D(FA)
get25DSlice(FA, des + file[:-9], dir)
def FA2Npy(source, file, des):
if not os.path.exists(des):
os.mkdir(des)
FA, affine = load_nifti(source + file)
# visualize3D(FA)
# get25DSlice(FA,des+file[:-9])
FA = np.array(FA)
print(FA.shape)
mid = FA.shape[-1] // 2
# vis_all(FA[:,:,mid-10:mid+10])
np.save(des + file[:-9], FA[:, :, mid])
def preDataset():
view = False
source = "/home/zhiyunl/MIA-Proj/DATA/HCP/unproc/"
desFA = "/home/zhiyunl/MIA-Proj/DATA/HCP/proc/"
desSlice = "/home/zhiyunl/MIA-Proj/DATA/HCP/16x3_mgh35/"
desNpy = "/home/zhiyunl/MIA-Proj/DATA/HCP/npy/"
if not os.path.exists(desFA):
os.mkdir(desFA)
for file in os.listdir(source):
if file.endswith(".bval"):
raw2FA(source, file, desFA)
# raw2FA(source,"LS3019_3T_DWI_dir80_LR.bval",des)
if not os.path.exists(desSlice):
os.mkdir(desSlice)
with open(source + '35MGH.csv', newline='') as csvfile:
labelreader = csv.reader(csvfile)
labelDict = {rows[0]: rows[2] for rows in labelreader}
mapping = {"20-24": 0, "25-29": 1, "30-34": 2, "35-39": 3, "40-44": 4, "45-59": 5}
# mapping = {"8-9": "young", "14-15": "young", "25-35": "old", "45-55": "old", "65-75": "old"}
for file in os.listdir(desFA):
# label = mapping[labelDict[file[:6]]] # LS2001
label = labelDict[file[:8]] # MGH_1001
if view:
FA, affine = load_nifti(source + file)
visualize3D(FA)
break
else:
FA2Slice(desFA, file, desSlice + label + "/", dir=3)
# if not os.path.exists(desNpy):
# os.mkdir(desNpy)
# for file in os.listdir(desFA):
# label = labelDict[file[:6]]
# FA2Npy(desFA,file,desNpy+label+"/")
if __name__ == "__main__":
preDataset()
# raw2FA_new("/home/zhiyunl/MIA-Proj/DATA/HCP/unproc/data.nii.gz",
# "/home/zhiyunl/MIA-Proj/DATA/HCP/unproc/bvals",
# "/home/zhiyunl/MIA-Proj/DATA/HCP/unproc/bvecs")