Fix scanner signature error and add binary signature verification#402
Fix scanner signature error and add binary signature verification#402albertx-wang wants to merge 4 commits into
Conversation
…port and verification * read_binary.py and write_binary.py: Added binary .bseq read/write support with MD5 signature serialization and deserialization. * read_seq.py and write_seq.py: Updated text .seq signature handling. write_seq.py now computes the MD5 hash from raw file bytes before appending [SIGNATURE], and read_seq.py restores the signature metadata consistently when reading signed text files. * verify_file_signature.py: Added a helper that recomputes the MD5 hash over the signed portion of text or binary Pulseq files and compares it with the stored signature, matching the MD5 verification behavior of MATLAB verifyFileSignature.m. * sequence.py: Exposed read_binary() and write_binary() on Sequence, and added shared binary section codes used by binary IO and signature verification. * test_binary_signature.py: Added binary signature roundtrip coverage for write_binary(), read_binary(), and verify_file_signature(), matching the binary signature roundtrip covered by MATLAB testBinarySignature.m.
|
Hi! Thanks for the PR. I noticed it fails on pre-commit, please make sure that the changes pass both pre-commit checks and tests before we start the review :) |
|
Hi! Thanks for the note! The pre-commit issue was due to a missing I originally changed the text |
| factor = float(_read_scalar(fid, np.float64)) | ||
| hint_len = int(_read_scalar(fid, np.int32)) | ||
| hint = fid.read(hint_len).decode('ascii') | ||
| if hint in seq.soft_delay_hint_ids: |
There was a problem hiding this comment.
Is there a reason hint ID logic here is different from the read_seq() and requires extra variables stored?
There was a problem hiding this comment.
The difference was to align with MATLAB’s binary read/write behavior. To avoid adding complexity here, I’ll move this into a separate PR.
There was a problem hiding this comment.
I see. It was a conscious decision on our part to slightly deviate from Matlab Pulseq's logic. You can see the relevant discussion here #264. I think it is best to mimic read_seq() function to avoid any behavioral differences.
| if 'Hash' in temp_sign_defs: | ||
| self.signature_value = temp_sign_defs['Hash'] | ||
| self.signature_file = 'Text' | ||
| self.signature_file = 'text' |
There was a problem hiding this comment.
I don't know if any code out there uses this variable, but upstream Pulseq kept this as 'Text', so might be a better idea to keep it as 'Text', even though it is inconsistent capitalizaiton with 'bin'
There was a problem hiding this comment.
Thanks for catching this, I will keep it as Text for consistency!
|
Thanks for the feedback and discussion. After discussing this, we think it would be cleaner for us to first submit a separate PR that only addresses the signature issue. We’ll keep that PR focused on the minimal signature fix, and split the other features into separate follow-up PRs. |
read_binary.pyandwrite_binary.py:Added binary
.bseqread/write support with MD5 signature serialization and deserialization.read_seq.pyandwrite_seq.py:Updated text
.seqsignature handling.write_seq.pynow computes the MD5 hash from raw file bytes before appending[SIGNATURE], andread_seq.pyrestores the signature metadata consistently when reading signed text files.verify_file_signature.py:Added a helper that recomputes the MD5 hash over the signed portion of text or binary Pulseq files and compares it with the stored signature, matching the MD5 verification behavior of MATLAB
verifyFileSignature.m.sequence.py:Exposed
read_binary()andwrite_binary()onSequence, and added shared binary section codes used by binary IO and signature verification.test_binary_signature.py:Added binary signature roundtrip coverage for
write_binary(),read_binary(), andverify_file_signature(), matching the binary signature roundtrip covered by MATLABtestBinarySignature.m.