-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_tse.py
304 lines (266 loc) · 9.43 KB
/
write_tse.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import math
import warnings
import numpy as np
import pypulseq as pp
def main(plot: bool, write_seq: bool, seq_filename: str = 'tse_pypulseq.seq'):
# ======
# SETUP
# ======
dG = 250e-6
# Set system limits
system = pp.Opts(
max_grad=32,
grad_unit='mT/m',
max_slew=130,
slew_unit='T/m/s',
rf_ringdown_time=100e-6,
rf_dead_time=100e-6,
adc_dead_time=10e-6,
)
seq = pp.Sequence(system) # Create a new sequence object
fov = 256e-3 # Define FOV and resolution
Nx, Ny = 64, 64
n_echo = 16 # Number of echoes
n_slices = 16
rf_flip = 180 # Flip angle
if isinstance(rf_flip, int):
rf_flip = np.zeros(n_echo) + rf_flip
slice_thickness = 5e-3
TE = 12e-3 # Echo time
TR = 5000e-3 # Repetition time
sampling_time = 6.4e-3
readout_time = sampling_time + 2 * system.adc_dead_time
t_ex = 2.5e-3
t_exwd = t_ex + system.rf_ringdown_time + system.rf_dead_time
t_ref = 2e-3
t_refwd = t_ref + system.rf_ringdown_time + system.rf_dead_time
t_sp = 0.5 * (TE - readout_time - t_refwd)
t_spex = 0.5 * (TE - t_exwd - t_refwd)
fsp_r = 1
fsp_s = 0.5
rf_ex_phase = np.pi / 2
rf_ref_phase = 0
# ======
# CREATE EVENTS
# ======
flip_ex = 90 * np.pi / 180
rf_ex, gz, _ = pp.make_sinc_pulse(
flip_angle=flip_ex,
system=system,
duration=t_ex,
slice_thickness=slice_thickness,
apodization=0.5,
time_bw_product=4,
phase_offset=rf_ex_phase,
return_gz=True,
)
gs_ex = pp.make_trapezoid(
channel='z',
system=system,
amplitude=gz.amplitude,
flat_time=t_exwd,
rise_time=dG,
)
flip_ref = rf_flip[0] * np.pi / 180
rf_ref, gz, _ = pp.make_sinc_pulse(
flip_angle=flip_ref,
system=system,
duration=t_ref,
slice_thickness=slice_thickness,
apodization=0.5,
time_bw_product=4,
phase_offset=rf_ref_phase,
use='refocusing',
return_gz=True,
)
gs_ref = pp.make_trapezoid(
channel='z',
system=system,
amplitude=gs_ex.amplitude,
flat_time=t_refwd,
rise_time=dG,
)
ags_ex = gs_ex.area / 2
gs_spr = pp.make_trapezoid(
channel='z',
system=system,
area=ags_ex * (1 + fsp_s),
duration=t_sp,
rise_time=dG,
)
gs_spex = pp.make_trapezoid(channel='z', system=system, area=ags_ex * fsp_s, duration=t_spex, rise_time=dG)
delta_k = 1 / fov
k_width = Nx * delta_k
gr_acq = pp.make_trapezoid(
channel='x',
system=system,
flat_area=k_width,
flat_time=readout_time,
rise_time=dG,
)
adc = pp.make_adc(num_samples=Nx, duration=sampling_time, delay=system.adc_dead_time)
gr_spr = pp.make_trapezoid(
channel='x',
system=system,
area=gr_acq.area * fsp_r,
duration=t_sp,
rise_time=dG,
)
agr_spr = gr_spr.area
agr_preph = gr_acq.area / 2 + agr_spr
gr_preph = pp.make_trapezoid(channel='x', system=system, area=agr_preph, duration=t_spex, rise_time=dG)
# Phase-encoding
n_ex = math.floor(Ny / n_echo)
pe_steps = np.arange(1, n_echo * n_ex + 1) - 0.5 * n_echo * n_ex - 1
if divmod(n_echo, 2)[1] == 0:
pe_steps = np.roll(pe_steps, [0, int(-np.round(n_ex / 2))])
pe_order = pe_steps.reshape((n_ex, n_echo), order='F').T
phase_areas = pe_order * delta_k
# Split gradients and recombine into blocks
gs1_times = np.array([0, gs_ex.rise_time])
gs1_amp = np.array([0, gs_ex.amplitude])
gs1 = pp.make_extended_trapezoid(channel='z', times=gs1_times, amplitudes=gs1_amp)
gs2_times = np.array([0, gs_ex.flat_time])
gs2_amp = np.array([gs_ex.amplitude, gs_ex.amplitude])
gs2 = pp.make_extended_trapezoid(channel='z', times=gs2_times, amplitudes=gs2_amp)
gs3_times = np.array(
[
0,
gs_spex.rise_time,
gs_spex.rise_time + gs_spex.flat_time,
gs_spex.rise_time + gs_spex.flat_time + gs_spex.fall_time,
]
)
gs3_amp = np.array([gs_ex.amplitude, gs_spex.amplitude, gs_spex.amplitude, gs_ref.amplitude])
gs3 = pp.make_extended_trapezoid(channel='z', times=gs3_times, amplitudes=gs3_amp)
gs4_times = np.array([0, gs_ref.flat_time])
gs4_amp = np.array([gs_ref.amplitude, gs_ref.amplitude])
gs4 = pp.make_extended_trapezoid(channel='z', times=gs4_times, amplitudes=gs4_amp)
gs5_times = np.array(
[
0,
gs_spr.rise_time,
gs_spr.rise_time + gs_spr.flat_time,
gs_spr.rise_time + gs_spr.flat_time + gs_spr.fall_time,
]
)
gs5_amp = np.array([gs_ref.amplitude, gs_spr.amplitude, gs_spr.amplitude, 0])
gs5 = pp.make_extended_trapezoid(channel='z', times=gs5_times, amplitudes=gs5_amp)
gs7_times = np.array(
[
0,
gs_spr.rise_time,
gs_spr.rise_time + gs_spr.flat_time,
gs_spr.rise_time + gs_spr.flat_time + gs_spr.fall_time,
]
)
gs7_amp = np.array([0, gs_spr.amplitude, gs_spr.amplitude, gs_ref.amplitude])
gs7 = pp.make_extended_trapezoid(channel='z', times=gs7_times, amplitudes=gs7_amp)
# Readout gradient
gr3 = gr_preph
gr5_times = np.array(
[
0,
gr_spr.rise_time,
gr_spr.rise_time + gr_spr.flat_time,
gr_spr.rise_time + gr_spr.flat_time + gr_spr.fall_time,
]
)
gr5_amp = np.array([0, gr_spr.amplitude, gr_spr.amplitude, gr_acq.amplitude])
gr5 = pp.make_extended_trapezoid(channel='x', times=gr5_times, amplitudes=gr5_amp)
gr6_times = np.array([0, readout_time])
gr6_amp = np.array([gr_acq.amplitude, gr_acq.amplitude])
gr6 = pp.make_extended_trapezoid(channel='x', times=gr6_times, amplitudes=gr6_amp)
gr7_times = np.array(
[
0,
gr_spr.rise_time,
gr_spr.rise_time + gr_spr.flat_time,
gr_spr.rise_time + gr_spr.flat_time + gr_spr.fall_time,
]
)
gr7_amp = np.array([gr_acq.amplitude, gr_spr.amplitude, gr_spr.amplitude, 0])
gr7 = pp.make_extended_trapezoid(channel='x', times=gr7_times, amplitudes=gr7_amp)
# Fill-times
t_ex = pp.calc_duration(gs1) + pp.calc_duration(gs2) + pp.calc_duration(gs3)
t_ref = pp.calc_duration(gs4) + pp.calc_duration(gs5) + pp.calc_duration(gs7) + readout_time
t_end = pp.calc_duration(gs4) + pp.calc_duration(gs5)
TE_train = t_ex + n_echo * t_ref + t_end
TR_fill = (TR - n_slices * TE_train) / n_slices
# Round to gradient raster
TR_fill = system.grad_raster_time * np.round(TR_fill / system.grad_raster_time)
if TR_fill < 0:
TR_fill = 1e-3
warnings.warn(f'TR too short, adapted to include all slices to: {1000 * n_slices * (TE_train + TR_fill)} ms')
else:
print(f'TR fill: {1000 * TR_fill} ms')
delay_TR = pp.make_delay(TR_fill)
# ======
# CONSTRUCT SEQUENCE
# ======
for k_ex in range(n_ex):
# deactivate dummies
# for k_ex in range(n_ex + 1):
for s in range(n_slices):
rf_ex.freq_offset = gs_ex.amplitude * slice_thickness * (s - (n_slices - 1) / 2)
rf_ref.freq_offset = gs_ref.amplitude * slice_thickness * (s - (n_slices - 1) / 2)
rf_ex.phase_offset = rf_ex_phase - 2 * np.pi * rf_ex.freq_offset * pp.calc_rf_center(rf_ex)[0]
rf_ref.phase_offset = rf_ref_phase - 2 * np.pi * rf_ref.freq_offset * pp.calc_rf_center(rf_ref)[0]
seq.add_block(gs1)
seq.add_block(gs2, rf_ex)
seq.add_block(gs3, gr3)
for k_echo in range(n_echo):
phase_area = phase_areas[k_echo, k_ex - 1]
# deactivate dummies
# if k_ex > 0:
# phase_area = phase_areas[k_echo, k_ex - 1]
# else:
# phase_area = 0.0 # 0.0 and not 0 because -phase_area should successfully result in negative zero
gp_pre = pp.make_trapezoid(
channel='y',
system=system,
area=phase_area,
duration=t_sp,
rise_time=dG,
)
gp_rew = pp.make_trapezoid(
channel='y',
system=system,
area=-phase_area,
duration=t_sp,
rise_time=dG,
)
seq.add_block(gs4, rf_ref)
seq.add_block(gs5, gr5, gp_pre)
# deactivate dummies
# if k_ex > 0:
# seq.add_block(gr6, adc)
# else:
# seq.add_block(gr6)
seq.add_block(gr6, adc)
seq.add_block(gs7, gr7, gp_rew)
seq.add_block(gs4)
seq.add_block(gs5)
seq.add_block(delay_TR)
(
ok,
error_report,
) = seq.check_timing() # Check whether the timing of the sequence is correct
if ok:
print('Timing check passed successfully')
else:
print('Timing check failed. Error listing follows:')
[print(e) for e in error_report]
# ======
# VISUALIZATION
# ======
if plot:
seq.plot()
# =========
# WRITE .SEQ
# =========
if write_seq:
seq.write(seq_filename)
pp.SAR.SAR_calc.calc_SAR(seq)
if __name__ == '__main__':
main(plot=False, write_seq=True)