@@ -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