forked from FabianRei/neuro_detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_FA_matrix.py
70 lines (42 loc) · 1.65 KB
/
create_FA_matrix.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
import os
from dipy.io.image import load_nifti, save_nifti
from dipy.io import read_bvals_bvecs
from dipy.core.gradients import gradient_table
from dipy.reconst.dti import TensorModel, color_fa
import numpy as np
import nibabel as nib
from dipy.reconst.dti import fractional_anisotropy
from PIL import Image
import pickle
def six2mat(voxel6):
return voxel6[[0, 1, 3, 1, 2, 4, 3, 4, 5]].reshape(3,3)
def getFaArr(fpath):
arr = nib.load(fpath).get_fdata().squeeze()
matArr = np.apply_along_axis(six2mat, -1, arr)
evalArr, evecArr = np.linalg.eig(matArr)
faArr = fractional_anisotropy(evalArr)
return faArr
fpath = '/home/reith/PycharmProjects/axisFlipDetector/data/tensors/5aac14517f233f00193eb257_tensors.nii.gz'
outputFolder = '/black/localhome/reith/Desktop/projects/Tensors/color_images/'
tensorPath = '/black/localhome/reith/Desktop/projects/Tensors/wh_tensors/'
fpaths = [os.path.join(tensorPath , p) for p in os.listdir(tensorPath)]
faArrs = []
for i, fpath in enumerate(fpaths):
faArrs.append(getFaArr(fpath))
print(f"{i} of {len(fpaths)} nifty files are processed!")
pickle.dump(faArrs, open("faArrList.p", "wb"))
print("done")
'''
fpath = '/home/reith/PycharmProjects/axisFlipDetector/dwi/'
fdwi = os.path.join(fpath, 'file.nii.gz')
fbval = os.path.join(fpath, 'file.bval')
fbvec = os.path.join(fpath, 'file.bvec')
data, affine = load_nifti(fdwi)
bvals, bvecs = read_bvals_bvecs(fbval, fbvec)
gtab = gradient_table(bvals, bvecs)
tenmodel = TensorModel(gtab)
tenfit = tenmodel.fit(data)
img = Image.fromarray((test[:,:,30,:]*255).astype(np.uint8))
img.show()
save_nifti('colorfa.nii.gz', tenfit.color_fa, affine)
'''