-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathepi_distortions.py
203 lines (159 loc) · 5.57 KB
/
epi_distortions.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
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
import numpy as np
import matplotlib.pyplot as plt
import math
#subprocess.call('git clone https://github.com/mikgroup/sigpy.git')
#subprocess.call('pip install sigpy')
import sigpy as sp
import sigpy.mri
import matplotlib.animation as manimation
import time
# Simulation paramaters
Tpe = 0.1*1e-3 # Time for blipped PE
Tread = 1.0*1e-3 # Readout time in ms
Npe = 128
Nfreq = 128
t_offset = 0.0
t = []
kx = []
ky = []
k_line = np.linspace(-Nfreq/2.0,Nfreq/2.0,Nfreq+2)
ky_all = np.linspace(-Npe/2.0,Npe/2.0,Npe)
t_line = np.linspace(0, Tread,Nfreq+2)
for pe in range(Npe):
if pe%2 == 0:
if pe > 0:
kx = np.concatenate([kx, k_line])
else:
kx = k_line
else:
if pe > 0:
kx = np.concatenate([kx, -k_line])
else:
kx = -k_line
ky = np.concatenate([ky,ky_all[pe]*np.ones(k_line.shape,k_line.dtype)])
t = np.concatenate([t,t_line+t_offset])
t_offset += Tpe + Tread
ky = -ky
# Plot Kx,Ky
plt.figure()
plt.plot(kx, ky)
# Kx/Ky vs time
plt.figure()
plt.plot(t, kx)
plt.plot(t, ky)
# Define fat as the outer ring
sl_amps = [1.0,-1.0]
sl_scales = [[.6900, .920, .810], # white big
[.6624, .874, .780]] # gray big
sl_offsets = [[0., 0., 0],
[0., -.0184, 0]]
sl_angles = [[0, 0, 0],
[0, 0, 0]]
fat = sp.sim.phantom([256,256], sl_amps, sl_scales, sl_offsets, sl_angles,dtype=np.complex64)
plt.figure()
plt.imshow(np.abs(fat))
sl_amps = [0.2, 1., 1.2, 1.1, 1.1, 1.1, 1.1, 1.1, 1.1]
sl_scales = [[.6624, .874, .780], # gray big
[.1100, .310, .220], # right black
[.1600, .410, .280], # left black
[.2100, .250, .410], # gray center blob
[.0460, .046, .050],
[.0460, .046, .050],
[.0460, .046, .050], # left small dot
[.0230, .023, .020], # mid small dot
[.0230, .023, .020]]
sl_offsets = [[0., -.0184, 0],
[.22, 0., 0],
[-.22, 0., 0],
[0., .35, -.15],
[0., .1, .25],
[0., -.1, .25],
[-.08, -.605, 0],
[0., -.606, 0],
[.06, -.605, 0]]
sl_angles = [[0, 0, 0],
[-18, 0, 10],
[18, 0, 10],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# Get complex phantom
water = sp.sim.phantom([256,256], sl_amps, sl_scales, sl_offsets, sl_angles,dtype=np.complex64)
[x,y] = np.meshgrid(np.linspace(-0.5,0.5,256),np.linspace(-0.5,0.5,256))
x0 = 0.0
y0 = 0.4
wY = 0.3
wX = 0.2
offmap = np.exp( -((x-x0)**2/(wX**2) + (y-y0)**2/(wY**2))**2)
# Subtract off mean
offmap -= np.mean(offmap)
offmap /= np.max(offmap)
offmap = np.flipud(offmap)
water = np.flipud(water)
fat = np.flipud(fat)
# Create gradients as triangles
plt.figure()
plt.imshow(offmap)
plt.draw()
plt.colorbar()
FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='epi_without_fat', artist='KMJ',
comment='EPI simulation with off-resonance')
writer = FFMpegWriter(fps=10, metadata=metadata)
# Do this on GPU
device = sp.backend.Device(0)
xp = device.xp
kx = sp.backend.to_device(kx, device)
ky = sp.backend.to_device(ky, device)
t = sp.backend.to_device(t, device)
water = sp.backend.to_device(water, device)
fat = sp.backend.to_device(fat, device)
offmap = sp.backend.to_device(offmap, device)
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(20, 10))
with writer.saving(fig, "epi_without_fat.mp4", 100):
for fmax in xp.linspace(-200.0,200.0,41):
with device:
t_start = time.time()
s = xp.zeros(kx.shape, xp.complex64)
img_est = xp.zeros(water.shape, xp.complex64)
# Now do the DFT
[x, y] = xp.meshgrid(xp.linspace(-0.5, 0.5, 256), xp.linspace(-0.5, 0.5, 256))
for pos in range(kx.shape[0]):
Gphase = xp.exp(1j*2.0*math.pi*(kx[pos]*x + ky[pos]*y))
Ophase = xp.exp(1j*2.0*math.pi*t[pos]*offmap*fmax)
s[pos] = xp.sum((water+0.0*fat*xp.exp(2j*math.pi*t[pos]*440))*Gphase*Ophase)
fr = 0.9*Nfreq/2.0
fw = 0.1*Nfreq/2.0
kr = xp.abs(kx[pos])
wx = 1. / (1. + xp.exp( (kr-fr)/fw))
kr = xp.abs(ky[pos])
wy = 1. / (1. + xp.exp( (kr-fr)/fw))
img_est += s[pos]*xp.conj(Gphase)*wx*wy
print(f'Took {time.time()-t_start}')
# Get Images
img_est_cpu = sp.backend.to_device(img_est,sp.cpu_device)
offmap_cpu = sp.backend.to_device(offmap,sp.cpu_device)
fmax_cpu = sp.backend.to_device(fmax,sp.cpu_device)
from mpl_toolkits.axes_grid1 import make_axes_locatable
def colorbar(mappable):
ax = mappable.axes
fig = ax.figure
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
return fig.colorbar(mappable, cax=cax)
img1 = ax1.imshow(fmax_cpu*offmap_cpu,cmap='Spectral')
img1.set_clim(-150,150)
colorbar(img1)
ax1.set_title('Off Resonance [Hz]')
ax1.axis('off')
img2 = ax2.imshow(np.abs(img_est_cpu),cmap='gray')
ax2.set_title('Estimated Image')
ax2.axis('off')
plt.tight_layout(h_pad=1)
plt.draw()
plt.pause(0.0001)
writer.grab_frame()
plt.show()