-
Notifications
You must be signed in to change notification settings - Fork 2
Initial commit #15
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
Draft
mbriggs134
wants to merge
1
commit into
develop
Choose a base branch
from
compare-miniseed
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Initial commit #15
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| from .msrecord_buffer_reader import MS3RecordBufferReader | ||
| from .msrecord import binary_compare_records, logically_compare_records | ||
|
|
||
|
|
||
| def sort_mseed_content(content: bytes) -> bytes: | ||
| """ | ||
| Sort miniSEED content bytes | ||
|
|
||
| Args: | ||
| content (bytes): The miniSEED content to sort. | ||
|
|
||
| Returns: | ||
| bytes: Sorted miniSEED content. | ||
| """ | ||
| record_array = bytearray(content) | ||
| source_id_starttime_record_triplet = [] | ||
| with MS3RecordBufferReader(record_array) as msreader: | ||
| for msr in msreader: | ||
| # Copy properties to avoid ctype issues | ||
| source_id_starttime_record_triplet.append( | ||
| ( | ||
| msr.sourceid, | ||
| msr.starttime, | ||
| msr.record, | ||
| ) | ||
| ) | ||
|
|
||
| # x[0]:source_id x[1]: starttime | ||
| source_id_starttime_record_triplet.sort(key=lambda x: (x[0], x[1])) | ||
|
|
||
| reordered_records = b"".join(r for _, _, r in source_id_starttime_record_triplet) | ||
|
|
||
| return reordered_records | ||
|
|
||
|
|
||
| def compare_miniseed_content( | ||
| content1: bytes, content2: bytes, ignore_order: bool = True | ||
| ) -> [bool, dict]: | ||
| """ | ||
| Compare two miniSEED content bytes for logical equality | ||
|
|
||
| Note if doing exact binary comparison, use `content1 == content2`. | ||
|
|
||
| Args: | ||
| content1 (bytes): First miniSEED content. | ||
| content2 (bytes): Second miniSEED content. | ||
|
|
||
| Returns: | ||
| bool: True if contents are equal, False otherwise. | ||
| """ | ||
|
|
||
| if ignore_order: | ||
| content1 = sort_mseed_content(content1) | ||
| content2 = sort_mseed_content(content2) | ||
|
|
||
| binary_compare_list = [] | ||
| logical_compare_list = [] | ||
| with ( | ||
| MS3RecordBufferReader( | ||
| bytearray(content1), unpack_data=True | ||
| ) as content1_msreader, | ||
| MS3RecordBufferReader( | ||
| bytearray(content2), unpack_data=True | ||
| ) as content2_msreader, | ||
| ): | ||
| for msr1, msr2 in zip(content1_msreader, content2_msreader): | ||
| binary_compare_list.append(binary_compare_records(msr1, msr2)) | ||
| logical_compare_list.append(logically_compare_records(msr1, msr2)) | ||
| return binary_compare_list, logical_compare_list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import pytest | ||
| import math | ||
|
|
||
|
|
||
| # A sine wave of 500 samples | ||
| sine_500 = list(map(lambda x: int(math.sin(math.radians(x)) * 500), range(0, 500))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from mseedlib import MS3Record | ||||||||||||||||||||||||||||||||||||||||||||||||||
| from mseedlib.msrecord_buffer_compare import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| sort_mseed_content, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| compare_miniseed_content, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| from .conftest import sine_500 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| # A global record buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||
| record_buffer = b"" | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.fixture(scope="module") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def msr_XX_TEST__B_S_X_bytes() -> tuple(MS3Record, bytes): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1 = MS3Record() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.set_starttime_str("2023-01-02T01:02:03.123456789Z") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.sourceid = "FDSN:XX_TEST__B_S_X" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.pack(record_handler, datasamples=sine_500, sampletype="i") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1_bytes = bytes(record_buffer) # Make a copy of the packed record | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return msr1, msr1_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.fixture(scope="module") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| def msr_XX_TEST__B_S_Y_bytes() -> tuple(MS3Record, bytes): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1 = MS3Record() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.set_starttime_str("2023-01-02T01:02:03.123456789Z") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.sourceid = "FDSN:XX_TEST__B_S_X" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.pack(record_handler, datasamples=sine_500, sampletype="i") | ||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1_bytes = bytes(record_buffer) # Make a copy of the packed record | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return msr1, msr1_bytes | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| def record_handler(record, handler_data): | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """A callback function for MS3Record.set_record_handler() | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Stores the record in a global buffer for testing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| print("Record handler called, record length: %d" % len(record)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| global record_buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||
| record_buffer = bytes(record) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+29
to
+40
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| msr1.pack(record_handler, datasamples=sine_500, sampletype="i") | |
| msr1_bytes = bytes(record_buffer) # Make a copy of the packed record | |
| return msr1, msr1_bytes | |
| def record_handler(record, handler_data): | |
| """A callback function for MS3Record.set_record_handler() | |
| Stores the record in a global buffer for testing | |
| """ | |
| print("Record handler called, record length: %d" % len(record)) | |
| global record_buffer | |
| record_buffer = bytes(record) | |
| handler_data = {} | |
| msr1.pack(record_handler, handler_data=handler_data, datasamples=sine_500, sampletype="i") | |
| msr1_bytes = handler_data["record_buffer"] # Make a copy of the packed record | |
| return msr1, msr1_bytes | |
| def record_handler(record, handler_data): | |
| """A callback function for MS3Record.set_record_handler() | |
| Stores the record in a context-local variable for testing | |
| """ | |
| print("Record handler called, record length: %d" % len(record)) | |
| handler_data["record_buffer"] = bytes(record) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fixture msr_XX_TEST__B_S_Y_bytes, the sourceid is set to "FDSN:XX_TEST__B_S_X" instead of a value reflecting the intended unique identity (e.g. "FDSN:XX_TEST__B_S_Y").