forked from mjallais/compute_RTOP
/
RTOP.py
50 lines (40 loc) · 1.53 KB
/
RTOP.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
#!/usr/bin/python
# Create RTOP file of image file on the mask passed in arguments
# arg 1 : path to dMRI image
# arg 2 : path to mask
# arg 3 : path to b-values
# arg 4 : path to b-vectors
from os import path
import numpy as np
from astropy import units
import nibabel as nib
from dipy.core import gradients
from dipy.reconst import mapmri
def compute_RTOP(dwi_, mask_, bvals_, bvecs_):
if path.exists(dwi_) and path.exists(mask_) and path.exists(bvals_) and path.exists(bvecs_):
dwi = nib.load(dwi_)
mask = nib.load(mask_)
bvals = np.loadtxt(bvals_) * units.s / units.mm**2
bvecs = np.loadtxt(bvecs_)
else:
print("One of the files does not exist, exit.")
exit()
dwi_data = dwi.get_data()
mask_data = mask.get_data()
nodif = dwi.slicer[:,:,:,0]
gtab = gradients.gradient_table_from_bvals_bvecs(bvals,
bvecs,
b0_threshold=5)
map_model = mapmri.MapmriModel(
gtab,
laplacian_regularization=True,
laplacian_weighting=0.2
)
map_fit = map_model.fit(dwi_data, mask=mask_data)
rtop = map_fit.rtop()
rtop_cortex_norm = rtop / rtop[mask_data!=0].mean()
rtop_nii = nib.Nifti1Image(rtop, affine=dwi.affine)
rtop_cortex_norm_nii = nib.Nifti1Image(rtop_cortex_norm, affine=dwi.affine)
rtop_nii.to_filename('RTOP_cortex.nii.gz')
rtop_cortex_norm_nii.to_filename('RTOP_cortex_norm.nii.gz')
return