Skip to content

Commit 04e833c

Browse files
committed
use EventLibrary.update()
1 parent 0feac13 commit 04e833c

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/pypulseq/Sequence/sequence.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -908,15 +908,23 @@ def mod_grad_axis(self, axis: str, modifier: float) -> None:
908908
raise RuntimeError('mod_grad_axis does not yet support the same gradient event used on multiple axes.')
909909

910910
for i in range(len(selected_events)):
911-
# Convert tuple to list for modification, then convert back
912-
grad_data = list(self.grad_library.data[selected_events[i]])
911+
event_id = selected_events[i]
912+
old_data = self.grad_library.data[event_id]
913+
grad_type = self.grad_library.type[event_id]
914+
915+
# Convert tuple to list for modification
916+
grad_data = list(old_data)
913917
grad_data[0] *= modifier
914-
if self.grad_library.type[selected_events[i]] == 'g' and len(grad_data) == 6:
918+
919+
if grad_type == 'g' and len(grad_data) == 6:
915920
# Need to update first and last fields for arbitrary gradients
916921
# Data structure: (amplitude, shape_ID1, shape_ID2, delay, first, last)
917922
grad_data[4] *= modifier # first
918923
grad_data[5] *= modifier # last
919-
self.grad_library.data[selected_events[i]] = tuple(grad_data)
924+
925+
# Use EventLibrary.update() to properly maintain keymap integrity
926+
new_data = tuple(grad_data)
927+
self.grad_library.update(event_id, old_data, new_data, grad_type)
920928

921929
# Clear block cache to ensure get_block() uses the modified gradient data
922930
if self.use_block_cache:

tests/test_mod_grad_axis.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,40 @@ def test_mod_grad_axis_no_gradients_on_axis():
283283
assert abs(block.gx.amplitude - 1000) < 1e-12, 'X gradient should be unchanged'
284284
assert abs(block.gz.amplitude - 1500) < 1e-12, 'Z gradient should be unchanged'
285285
assert not hasattr(block, 'gy') or block.gy is None, 'Y gradient should not exist'
286+
287+
288+
def test_mod_grad_axis_keymap_integrity():
289+
"""Test that EventLibrary keymap integrity is maintained after modification."""
290+
seq = pp.Sequence()
291+
292+
# Create a gradient and add it to the sequence
293+
gx = pp.make_trapezoid('x', amplitude=1000, flat_time=5e-3)
294+
seq.add_block(gx)
295+
296+
# Store original library state
297+
original_keymap_size = len(seq.grad_library.keymap)
298+
original_data_size = len(seq.grad_library.data)
299+
300+
# Modify the gradient
301+
seq.mod_grad_axis('x', -1.0)
302+
303+
# Check that library sizes are maintained (no duplicates created)
304+
assert len(seq.grad_library.keymap) == original_keymap_size, 'Keymap size should be unchanged'
305+
assert len(seq.grad_library.data) == original_data_size, 'Data size should be unchanged'
306+
307+
# Verify that a modification does not create a duplicate gradient event
308+
gx_inverted = pp.make_trapezoid('x', amplitude=-1000, flat_time=5e-3)
309+
seq.add_block(gx_inverted)
310+
311+
# Library should still have the same number of unique gradients
312+
assert len(seq.grad_library.data) == original_data_size, 'No duplicate gradient should be created'
313+
314+
# Verify that both gradients reference the same gradient event
315+
block1_events = seq.block_events[1]
316+
block2_events = seq.block_events[2]
317+
318+
# The gradient event ID is at index 2
319+
gx_event_id_block1 = block1_events[2]
320+
gx_event_id_block2 = block2_events[2]
321+
322+
assert gx_event_id_block1 == gx_event_id_block2, 'Both gradients should reference the same gradient event'

0 commit comments

Comments
 (0)