Skip to content

Commit 9c57d9f

Browse files
h3lg3schuenke
andauthored
Consider floating point accuracy for system specs checks (#270)
* - when checked for system violations of sequence, floating point accuracy is considered * - changed >= to > - added eps to avoid floating point rounding issues --------- Co-authored-by: Patrick Schuenke <37338697+schuenke@users.noreply.github.com>
1 parent 50f63ec commit 9c57d9f

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

src/pypulseq/make_arbitrary_grad.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import numpy as np
55

6+
from pypulseq import eps
67
from pypulseq.opts import Opts
78
from pypulseq.utils.tracing import trace, trace_enabled
89

@@ -76,9 +77,9 @@ def make_arbitrary_grad(
7677
raise ValueError(f'Invalid channel. Must be one of x, y or z. Passed: {channel}')
7778

7879
slew_rate = np.diff(waveform) / system.grad_raster_time
79-
if max(abs(slew_rate)) >= max_slew:
80+
if max(abs(slew_rate)) > max_slew + eps:
8081
raise ValueError(f'Slew rate violation {max(abs(slew_rate)) / max_slew * 100}')
81-
if max(abs(waveform)) >= max_grad:
82+
if max(abs(waveform)) > max_grad + eps:
8283
raise ValueError(f'Gradient amplitude violation {max(abs(waveform)) / max_grad * 100}')
8384

8485
if first is None:

src/pypulseq/make_extended_trapezoid_area.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import numpy as np
55

6+
from pypulseq import eps
67
from pypulseq.make_extended_trapezoid import make_extended_trapezoid
78
from pypulseq.opts import Opts
89
from pypulseq.utils.cumsum import cumsum
@@ -87,7 +88,7 @@ def _find_solution(duration: int) -> Union[None, Tuple[int, int, int, float]]:
8788

8889
# Check if gradient amplitude exceeds max_grad, if so, adjust ramp
8990
# times for a trapezoidal gradient with maximum slew rate.
90-
if grad_start + ramp_up_time * max_slew * raster_time > max_grad:
91+
if grad_start + ramp_up_time * max_slew * raster_time > max_grad + eps:
9192
ramp_up_time = round(_calc_ramp_time(grad_start, max_grad) / raster_time)
9293
ramp_down_time = round(_calc_ramp_time(grad_end, max_grad) / raster_time)
9394
else:
@@ -105,7 +106,7 @@ def _find_solution(duration: int) -> Union[None, Tuple[int, int, int, float]]:
105106

106107
# Check if gradient amplitude exceeds -max_grad, if so, adjust ramp
107108
# times for a trapezoidal gradient with maximum slew rate.
108-
if grad_start - ramp_up_time * max_slew * raster_time < -max_grad:
109+
if grad_start - ramp_up_time * max_slew * raster_time < -max_grad - eps:
109110
ramp_up_time = round(_calc_ramp_time(grad_start, -max_grad) / raster_time)
110111
ramp_down_time = round(_calc_ramp_time(grad_end, -max_grad) / raster_time)
111112
else:

src/pypulseq/make_trapezoid.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def calculate_shortest_params_for_area(area: float, max_slew: float, max_grad: f
2020
effective_time = rise_time
2121

2222
# Adjust for max gradient constraint
23-
if abs(amplitude) > max_grad:
23+
if abs(amplitude) > max_grad + eps:
2424
effective_time = math.ceil(abs(area) / max_grad / grad_raster_time) * grad_raster_time
2525
amplitude = area / effective_time
2626
rise_time = math.ceil(abs(amplitude) / max_slew / grad_raster_time) * grad_raster_time
@@ -225,15 +225,15 @@ def make_trapezoid(
225225
if rise_time is None and fall_time is None:
226226
rise_time = fall_time = calculate_shortest_rise_time(amplitude2, max_slew, system.grad_raster_time)
227227

228-
if abs(amplitude2) > max_grad:
228+
if abs(amplitude2) > max_grad + eps:
229229
raise ValueError(f'Refined amplitude ({abs(amplitude2):0.0f} Hz/m) is larger than max ({max_grad:0.0f} Hz/m).')
230230

231-
if abs(amplitude2) / rise_time > max_slew:
231+
if abs(amplitude2) / rise_time > max_slew + eps:
232232
raise ValueError(
233233
f'Refined slew rate ({abs(amplitude2) / rise_time:0.0f} Hz/m/s) for ramp up is larger than max ({max_slew:0.0f} Hz/m/s).'
234234
)
235235

236-
if abs(amplitude2) / fall_time > max_slew:
236+
if abs(amplitude2) / fall_time > max_slew + eps:
237237
raise ValueError(
238238
f'Refined slew rate ({abs(amplitude2) / fall_time:0.0f} Hz/m/s) for ramp down is larger than max ({max_slew:0.0f} Hz/m/s).'
239239
)

0 commit comments

Comments
 (0)