From 72010259b67da23f9f33be19c89da2cc7cb84c45 Mon Sep 17 00:00:00 2001 From: Frank Zijlstra <22915457+FrankZijlstra@users.noreply.github.com> Date: Mon, 5 Feb 2024 11:28:46 +0100 Subject: [PATCH 1/6] Added missing `area` calculation to `make_extended_trapezoid` --- pypulseq/make_extended_trapezoid.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pypulseq/make_extended_trapezoid.py b/pypulseq/make_extended_trapezoid.py index f460ce7..dc61044 100644 --- a/pypulseq/make_extended_trapezoid.py +++ b/pypulseq/make_extended_trapezoid.py @@ -139,6 +139,7 @@ def make_extended_trapezoid( grad.shape_dur = ( round(times[-1] / system.grad_raster_time) * system.grad_raster_time ) + grad.area = 0.5 * ((grad.tt[1:] - grad.tt[:-1]) * (grad.waveform[1:] + grad.waveform[:-1])).sum() grad.first = amplitudes[0] grad.last = amplitudes[-1] From 20358a5a6b91cc81fb3939ecfda34e4c1d2fb668 Mon Sep 17 00:00:00 2001 From: Frank Zijlstra <22915457+FrankZijlstra@users.noreply.github.com> Date: Mon, 5 Feb 2024 11:29:09 +0100 Subject: [PATCH 2/6] Fixed bug in `add_gradients` Also cleaned up some unnecessary function calls. --- pypulseq/add_gradients.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pypulseq/add_gradients.py b/pypulseq/add_gradients.py index bc273bd..6327ea8 100644 --- a/pypulseq/add_gradients.py +++ b/pypulseq/add_gradients.py @@ -102,7 +102,7 @@ def add_gradients( else: times.extend(g.delay + g.tt) - times = np.sort(np.unique(times)) + times = np.unique(times) dt = times[1:] - times[:-1] ieps = np.flatnonzero(dt < eps) if np.any(ieps): @@ -118,10 +118,10 @@ def add_gradients( g = grads[ii] if g.type == "trap": if g.flat_time > 0: # Trapezoid or triangle - tt = list(cumsum(g.delay, g.rise_time, g.flat_time, g.fall_time)) + tt = cumsum(g.delay, g.rise_time, g.flat_time, g.fall_time) waveform = [0, g.amplitude, g.amplitude, 0] else: - tt = list(cumsum(g.delay, g.rise_time, g.fall_time)) + tt = cumsum(g.delay, g.rise_time, g.fall_time) waveform = [0, g.amplitude, 0] else: tt = g.delay + g.tt @@ -140,7 +140,7 @@ def add_gradients( if abs(waveform[0]) > eps and tt[0] > eps: tt[0] += eps - amplitudes += np.interp(xp=tt, fp=waveform, x=times) + amplitudes += np.interp(xp=tt, fp=waveform, x=times, left=0, right=0) grad = make_extended_trapezoid( channel=channel, amplitudes=amplitudes, times=times, system=system From a22879a60671c2cb6abfd46cbf52adf45a57ee8f Mon Sep 17 00:00:00 2001 From: Frank Zijlstra <22915457+FrankZijlstra@users.noreply.github.com> Date: Mon, 5 Feb 2024 18:22:57 +0100 Subject: [PATCH 3/6] Fixed incorrect `shape_dur` in `make_extended_trapezoid` when a delay is specified in the `times` argument --- pypulseq/make_extended_trapezoid.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pypulseq/make_extended_trapezoid.py b/pypulseq/make_extended_trapezoid.py index dc61044..17effb7 100644 --- a/pypulseq/make_extended_trapezoid.py +++ b/pypulseq/make_extended_trapezoid.py @@ -136,9 +136,7 @@ def make_extended_trapezoid( round(times[0] / system.grad_raster_time) * system.grad_raster_time ) grad.tt = times - grad.delay - grad.shape_dur = ( - round(times[-1] / system.grad_raster_time) * system.grad_raster_time - ) + grad.shape_dur = grad.tt[-1] grad.area = 0.5 * ((grad.tt[1:] - grad.tt[:-1]) * (grad.waveform[1:] + grad.waveform[:-1])).sum() grad.first = amplitudes[0] From a3cdf82e55d70cb2731cb62d93c4379f8c244db3 Mon Sep 17 00:00:00 2001 From: Frank Zijlstra <22915457+FrankZijlstra@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:39:16 +0100 Subject: [PATCH 4/6] Fixed a bug in `event_lib` which could give KeyError's when duplicate events exist in a .seq file --- pypulseq/event_lib.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pypulseq/event_lib.py b/pypulseq/event_lib.py index bf300de..2d05624 100755 --- a/pypulseq/event_lib.py +++ b/pypulseq/event_lib.py @@ -219,7 +219,12 @@ def update( data_type : str, default=str() """ if key_id in self.data: - del self.keymap[self.data[key_id]] + # TODO: When reading a sequence file we can end up with duplicate + # events due to rounding to 6 digits. update() calls to both + # duplicates would give a key error here on the second + # duplicate. Ideally no duplicates should exist though... + if self.data[key_id] in self.keymap: + del self.keymap[self.data[key_id]] self.insert(key_id, new_data, data_type) From 6a5109d8a5bf2f5315d170c993b73a01f2eec9fb Mon Sep 17 00:00:00 2001 From: Frank Zijlstra <22915457+FrankZijlstra@users.noreply.github.com> Date: Wed, 7 Feb 2024 13:23:47 +0100 Subject: [PATCH 5/6] Revert cleanup in `add_gradients` because it broke other code... --- pypulseq/add_gradients.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pypulseq/add_gradients.py b/pypulseq/add_gradients.py index 6327ea8..d1f8106 100644 --- a/pypulseq/add_gradients.py +++ b/pypulseq/add_gradients.py @@ -118,10 +118,10 @@ def add_gradients( g = grads[ii] if g.type == "trap": if g.flat_time > 0: # Trapezoid or triangle - tt = cumsum(g.delay, g.rise_time, g.flat_time, g.fall_time) + tt = list(cumsum(g.delay, g.rise_time, g.flat_time, g.fall_time)) waveform = [0, g.amplitude, g.amplitude, 0] else: - tt = cumsum(g.delay, g.rise_time, g.fall_time) + tt = list(cumsum(g.delay, g.rise_time, g.fall_time)) waveform = [0, g.amplitude, 0] else: tt = g.delay + g.tt From a9d1e0329482378e2599d98e1ed4acf239eabbba Mon Sep 17 00:00:00 2001 From: Frank Zijlstra <22915457+FrankZijlstra@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:48:45 +0100 Subject: [PATCH 6/6] Fixed `Opts` to work properly with negative `gamma` (e.g. for Oxygen-17) --- pypulseq/opts.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pypulseq/opts.py b/pypulseq/opts.py index 7002895..0274ad5 100755 --- a/pypulseq/opts.py +++ b/pypulseq/opts.py @@ -82,14 +82,14 @@ def __init__( if max_grad != None: max_grad = convert( - from_value=max_grad, from_unit=grad_unit, to_unit="Hz/m", gamma=gamma + from_value=max_grad, from_unit=grad_unit, to_unit="Hz/m", gamma=abs(gamma) ) else: max_grad = Opts.default.max_grad if max_slew != None: max_slew = convert( - from_value=max_slew, from_unit=slew_unit, to_unit="Hz/m", gamma=gamma + from_value=max_slew, from_unit=slew_unit, to_unit="Hz/m", gamma=abs(gamma) ) else: max_slew = Opts.default.max_slew