-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsignal_temp_equalizer.py
More file actions
executable file
·215 lines (147 loc) · 6.96 KB
/
signal_temp_equalizer.py
File metadata and controls
executable file
·215 lines (147 loc) · 6.96 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import nibabel as nib
import pylab as pl
import argparse
from dipy.io.gradients import read_bvals_bvecs
# from scilpy.utils.bvec_bval_tools import (normalize_bvecs, is_normalized_bvecs)
from dipy.core.gradients import gradient_table
import dipy.reconst.dti as dti
desciption = """
Global intensity correction for temperature difference.
1) Fit DTI on temperature-stable volume subset
2) Estimate temperature of each volume from disparity with stabe subset fit
2a) TODO: smooth or model the temperature as function of time
3) Scale each volume for the main mean diffusivity (estimated as 1/bval)
"""
def _build_args_parser():
p = argparse.ArgumentParser(description=desciption, formatter_class=argparse.RawTextHelpFormatter)
p.add_argument('data', metavar='data', help='Path of the dwi nifti file.')
p.add_argument('bval', metavar='bval', help='Path of the bval file.')
p.add_argument('bvec', metavar='bvec', help='Path of the bvec file.')
p.add_argument('output', metavar='output', help='Path of the output nifti.')
p.add_argument('outputks', metavar='output', help='Path of the diffusivity multiplier map.')
# requires either a file with volume mask or a number of last volume
group = p.add_mutually_exclusive_group(required=True)
group.add_argument('--index', metavar='index', help='Path to files with 1s in the position of volume to include')
group.add_argument('--last', metavar='last', type=int, help='Number of non-b0 volume to include starting from the end.')
p.add_argument('--mask', metavar='mask', help='Path of the brain mask for normalization.')
return p
def main():
parser = _build_args_parser()
args = parser.parse_args()
print('LOADING DATA')
# load data
img = nib.load(args.data)
data = img.get_fdata()
# load bval bvec
bval, bvec = read_bvals_bvecs(args.bval, args.bvec)
gtab = gradient_table(bval, bvec)
# detect b0
b0_th = 75.
b0_index = np.where(bval < b0_th)[0]
# build include index
if args.index is None:
index = np.zeros(bval.shape[0], dtype=np.bool)
# include the last args.last non-b0
N = args.last
# list of non-zero index
tmp = np.arange(bval.shape[0])[bval >= b0_th]
index[tmp[-N:]] = True
# include all b0s
index[b0_index] = True
else:
index = np.genfromtxt(args.index).astype(np.bool)
if index.shape[0] != bval.shape[0]:
print('index length different from bval length')
return None
if args.mask is None:
mask = np.ones(data.shape[:3], dtype=np.bool)
else:
mask = nib.load(args.mask).get_fdata().astype(np.bool)
totalVoxel = np.prod(mask.shape)
voxelInMask = mask.sum()
print('{} voxels out of {} inside mask ({:.1f}%)'.format(voxelInMask, totalVoxel, 100*voxelInMask/totalVoxel))
print('COMPUTING TIME CURVES')
# compute per-volume mean inside mask
volume_mean_intensity = data[mask].mean(axis=0)
viz_hack = volume_mean_intensity.copy()
viz_hack[b0_index] = np.nan
pl.figure()
pl.plot(volume_mean_intensity, color='black', label='mean intensity in mask')
pl.scatter(np.where(index), np.ones(index.sum())*(0.95*volume_mean_intensity.min()), label='Included', color='red')
pl.title('Mean intensity in mask (WITH b0)')
pl.legend()
pl.figure()
pl.plot(viz_hack, color='black', label='mean intensity in mask')
pl.scatter(np.where(index), np.ones(index.sum())*(0.95*volume_mean_intensity.min()), label='Included', color='red')
pl.title('Mean intensity in mask (WITHOUT b0)')
pl.legend()
pl.show()
# fit DTI on included volume in index
low_bvec = bvec[index]
low_bval = bval[index]
data_low = data[..., index]
gtab_low = gradient_table(low_bval, low_bvec)
tenmodel_low = dti.TensorModel(gtab_low)
print('FITTING DTI')
tenmodel_low = dti.TensorModel(gtab_low, fit_method='WLS', return_S0_hat=True)
# Dipy has hard min signal cutoff of 1e-4, so we amplify
# set the non-zero minimum to the threshold
data_low_masked = data_low[mask]
# this is gettho more and more garbage, to accomodate debias negative data
# we look at the min above zero
# in pratice this is terrible and we should just scale the data before
multiplier = 1e-4 / data_low_masked[(data_low_masked>0)].min()
tenfit_low = tenmodel_low.fit(multiplier*data_low, mask=mask)
print('PREDICTING SIGNALS')
# predict signal for each volume for fit
predicted_S0 = tenfit_low.S0_hat
predicted_signal = tenfit_low.predict(gtab)
predicted_normalized_signal = predicted_signal / predicted_S0[..., None]
predicted_normalized_signal[np.isnan(predicted_normalized_signal)] = 0
predicted_normalized_signal[np.isinf(predicted_normalized_signal)] = 0
predicted_adc = -np.log(predicted_normalized_signal)/bval[None, None, None, :]
predicted_adc[np.isinf(predicted_adc)] = 0
predicted_adc[np.isnan(predicted_adc)] = 0
print('COMPUTING TEMPERATURE')
ks = 1 - (np.log(multiplier*data/(predicted_signal)) * (bval[None, None, None, :]*predicted_adc)**-1)
ks[np.isnan(ks)] = 1
ks[np.isinf(ks)] = 1
# save the diffusivity multiplier map
nib.nifti1.Nifti1Image(ks, img.affine).to_filename(args.outputks)
mean_directional_k = np.median(ks[mask], axis=(0,))
pl.figure()
pl.plot(mean_directional_k )
pl.axhline(1, color='red', linestyle='dashed')
pl.title('Estimated (median) diffusivity multiplier in mask per volume')
pl.show()
print('COMPUTING CORRECTION')
# diffusivity to center the calibration
D_calibrate = 1/bval.max()
print('Calibrating for diffusivity 1/bmax = 1/{:.0f} = {:.2e}'.format(bval.max(), D_calibrate))
heat_calibrate = np.exp(-bval*mean_directional_k*D_calibrate)
# pl.plot(np.exp(-bval*D_calibrate)/heat_calibrate)
meank_fix_correction = np.exp(-bval*D_calibrate)/heat_calibrate
data_meank_fix = data * meank_fix_correction[None,None,None,:]
meandata_meank_fix = data_meank_fix[mask].mean(axis=(0,))
viz_hack2 = meandata_meank_fix.copy()
viz_hack2[b0_index] = np.nan
pl.figure()
pl.plot(volume_mean_intensity, color='black', label='no correction')
pl.plot(meandata_meank_fix, color='red', label='with correction')
# pl.scatter(np.where(index), np.ones(index.sum())*(0.95*volume_mean_intensity.min()), label='Included', color='red')
pl.title('Mean intensity in mask (WITH b0)')
pl.legend()
pl.figure()
pl.plot(viz_hack, color='black', label='no correction')
pl.plot(viz_hack2, color='red', label='with correction')
# pl.scatter(np.where(index), np.ones(index.sum())*(0.95*volume_mean_intensity.min()), label='Included', color='red')
pl.title('Mean intensity in mask (WITHOUT b0)')
pl.legend()
pl.show()
print('SAVING CORRECTED NIFTI')
nib.nifti1.Nifti1Image(data_meank_fix, img.affine).to_filename(args.output)
if __name__ == "__main__":
main()