Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to remove duplicate events #178

Merged
merged 13 commits into from
Jun 10, 2024

Conversation

FrankZijlstra
Copy link
Collaborator

This PR provides a basic fix for issue #169. Both Sequence and EventLibrary now have a remove_duplicates function that removes events that would be identical after rounding. The function is called both before Sequence.write and after Sequence.read to ensure that no actual duplicate events exist in the event libraries.

I opted not to do any rounding while adding events, because this affects all sequence analysis functions (e.g. calculate_kspace), and it affects any use of get_block to copy events (i.e. there can be compounding rounding errors if events are stored rounded). The proposed solution is also a lot faster.

This does mean that in the example of #169, the library will still contain a lot of events, which will only be filtered when writing the sequence. Alternatively, seq.remove_duplicates(in_place=True) could be called manually at any point during the creation of a sequence to force rounding of the event libraries.

In addition, I added a basis for Sequence unittests, testing whether sequences can be write and read back in, and recreated by add_block(get_block(...)), and still produce (within rounding errors) the same sequence (tested with get_block, get_gradients, and calculate_kspace). It also checks the .seq output compared to a known baseline, contained in the tests/expected_output directory. The expected .seq output files can be regenerated with the current repository by setting a SAVE_EXPECTED environment variable before running pytest (e.g. if a change in the output happens that is confirmed to be correct).

These tests should be expanded with further sequences snippets which tests all pypulseq functionality, and e.g. including all current example sequences.

Adding these tests uncovered two small bugs fixes in Sequence that I included here.

- Added `remove_duplicates` in `event_lib`
- Added `remove_duplicates` in `Sequence`
- Changed `write_seq` to call `remove_duplicates` before writing
- Changed `read_seq` to call `remove_duplicates` after reading
…urn 0 instead of nan outside the specified gradients
Copy link
Collaborator

@schuenke schuenke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the Code with the problematic sequence from #169 and everyhing works fine. I didn't check all tests in detail, but the idea totally makes sense to me.

My comments are not meant as necessary changes, but only as a suggestion for better readability/debugging.

pypulseq/Sequence/sequence.py Outdated Show resolved Hide resolved
pypulseq/Sequence/sequence.py Outdated Show resolved Hide resolved
pypulseq/Sequence/sequence.py Outdated Show resolved Hide resolved
pypulseq/event_lib.py Outdated Show resolved Hide resolved
@schuenke schuenke merged commit 40f5936 into imr-framework:dev Jun 10, 2024
4 checks passed
@schuenke schuenke mentioned this pull request Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants