-
Notifications
You must be signed in to change notification settings - Fork 35
702: Add regex support for target sorting in target_is_sorted_by operator #1705
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4ddf1a4
Add regex support for target sorting in target_is_sorted_by operator
alexfurmenkov 74bf864
Remove the deprecated function
alexfurmenkov 3b65d2c
Enhance target_is_sorted_by operator documentation with regex paramet…
alexfurmenkov b19cb47
Update merged schema files with markdown descriptions
eb47752
Merge branch 'main' into 702-target-is-sorted-by-regex
alexfurmenkov 579fd86
Fix formatting of regex comment in target_is_sorted_by operator docum…
alexfurmenkov 106a725
Merge branch '702-target-is-sorted-by-regex' of https://github.com/cd…
alexfurmenkov 68e9141
Update merged schema files with markdown descriptions
02cbb87
Merge branch 'main' into 702-target-is-sorted-by-regex
RamilCDISC 6715e04
Enhance target sorting in target_is_sorted_by operator to include ext…
alexfurmenkov 66c8c58
Merge branch '702-target-is-sorted-by-regex' of https://github.com/cd…
alexfurmenkov 0041445
Merge branch 'main' into 702-target-is-sorted-by-regex
RamilCDISC 8941d97
Merge branch 'main' into 702-target-is-sorted-by-regex
SFJohnson24 f3792af
Merge branch 'main' into 702-target-is-sorted-by-regex
SFJohnson24 79010a1
Merge branch 'main' into 702-target-is-sorted-by-regex
SFJohnson24 e51ceae
Merge branch 'main' into 702-target-is-sorted-by-regex
SFJohnson24 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
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
185 changes: 185 additions & 0 deletions
185
tests/unit/test_check_operators/test_target_is_sorted_by_regex.py
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,185 @@ | ||
| """ | ||
| Tests for target_is_sorted_by operator with regex support | ||
| """ | ||
|
|
||
| import pytest | ||
| import pandas as pd | ||
| from cdisc_rules_engine.check_operators.dataframe_operators import DataframeType | ||
| from cdisc_rules_engine.models.dataset.pandas_dataset import PandasDataset | ||
| from cdisc_rules_engine.models.dataset.dask_dataset import DaskDataset | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dataset_class", [PandasDataset, DaskDataset]) | ||
| def test_target_is_sorted_by_with_regex_non_padded(dataset_class): | ||
| """ | ||
| Test target_is_sorted_by with regex extraction for non-zero-padded sequence numbers. | ||
| Example: lalala1, lalala2, ..., lalala9, lalala10 | ||
| """ | ||
| df = dataset_class.from_dict( | ||
| { | ||
| "USUBJID": ["001", "001", "001", "001", "002", "002", "002"], | ||
| "MIDSTYPE": ["A", "A", "A", "A", "B", "B", "B"], | ||
| "MIDS": [ | ||
| "lalala1", | ||
| "lalala2", | ||
| "lalala9", | ||
| "lalala10", | ||
| "test1", | ||
| "test2", | ||
| "test10", | ||
| ], | ||
| "SMSTDTC": [ | ||
| "2020-01-01", | ||
| "2020-01-02", | ||
| "2020-01-09", | ||
| "2020-01-10", | ||
| "2020-02-01", | ||
| "2020-02-02", | ||
| "2020-02-10", | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| other_value = { | ||
| "target": "MIDS", | ||
| "regex": ".*?(\\d+)$", # Non-greedy to correctly extract multi-digit numbers | ||
| "within": ["USUBJID", "MIDSTYPE"], | ||
| "comparator": [ | ||
| {"name": "SMSTDTC", "sort_order": "ASC", "null_position": "first"} | ||
| ], | ||
| } | ||
|
|
||
| result = DataframeType({"value": df}).target_is_sorted_by(other_value) | ||
| # All should be True - sorted correctly by chronological order | ||
| assert result.equals(pd.Series([True, True, True, True, True, True, True])) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dataset_class", [PandasDataset, DaskDataset]) | ||
| def test_target_is_sorted_by_with_regex_zero_padded(dataset_class): | ||
| """ | ||
| Test target_is_sorted_by with regex extraction for zero-padded sequence numbers. | ||
| Example: lalala01, lalala02, ..., lalala09, lalala10 | ||
| """ | ||
| df = dataset_class.from_dict( | ||
| { | ||
| "USUBJID": ["001", "001", "001", "001"], | ||
| "MIDSTYPE": ["A", "A", "A", "A"], | ||
| "MIDS": ["lalala01", "lalala02", "lalala09", "lalala10"], | ||
| "SMSTDTC": [ | ||
| "2020-01-01", | ||
| "2020-01-02", | ||
| "2020-01-09", | ||
| "2020-01-10", | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| other_value = { | ||
| "target": "MIDS", | ||
| "regex": ".*?(\\d+)$", # Non-greedy to correctly extract multi-digit numbers | ||
| "within": ["USUBJID", "MIDSTYPE"], | ||
| "comparator": [ | ||
| {"name": "SMSTDTC", "sort_order": "ASC", "null_position": "first"} | ||
| ], | ||
| } | ||
|
|
||
| result = DataframeType({"value": df}).target_is_sorted_by(other_value) | ||
| # All should be True - numeric conversion handles zero-padding | ||
| assert result.equals(pd.Series([True, True, True, True])) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dataset_class", [PandasDataset, DaskDataset]) | ||
| def test_target_is_sorted_by_with_regex_invalid_order(dataset_class): | ||
| """ | ||
| Test that invalid order is detected even with regex extraction. | ||
| """ | ||
| df = dataset_class.from_dict( | ||
| { | ||
| "USUBJID": ["001", "001", "001", "001"], | ||
| "MIDSTYPE": ["A", "A", "A", "A"], | ||
| "MIDS": ["lalala1", "lalala10", "lalala2", "lalala9"], | ||
| "SMSTDTC": [ | ||
| "2020-01-01", | ||
| "2020-01-02", | ||
| "2020-01-09", | ||
| "2020-01-10", | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| other_value = { | ||
| "target": "MIDS", | ||
| "regex": ".*?(\\d+)$", # Non-greedy to correctly extract multi-digit numbers | ||
| "within": ["USUBJID", "MIDSTYPE"], | ||
| "comparator": [ | ||
| {"name": "SMSTDTC", "sort_order": "ASC", "null_position": "first"} | ||
| ], | ||
| } | ||
|
|
||
| result = DataframeType({"value": df}).target_is_sorted_by(other_value) | ||
| # After sorting by extracted MIDS (1, 2, 9, 10), dates should be: | ||
| # MIDS=1 (2020-01-01) -> MIDS=2 (should be 2020-01-02) -> MIDS=9 (should be 2020-01-09) -> MIDS=10 (should be 2020-01-10) | ||
| # Actual dates: 2020-01-01, 2020-01-09, 2020-01-10, 2020-01-02 | ||
| # Only MIDS=1 is in correct chronological position | ||
| assert result.equals(pd.Series([True, False, False, False])) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dataset_class", [PandasDataset, DaskDataset]) | ||
| def test_target_is_sorted_by_with_regex_multiple_groups(dataset_class): | ||
| """ | ||
| Test regex sorting with multiple USUBJID and MIDSTYPE groups. | ||
| """ | ||
| df = dataset_class.from_dict( | ||
| { | ||
| "USUBJID": ["001", "001", "001", "002", "002", "002"], | ||
| "MIDSTYPE": ["A", "A", "A", "A", "A", "A"], | ||
| "MIDS": ["M1", "M2", "M3", "M1", "M2", "M3"], | ||
| "SMSTDTC": [ | ||
| "2020-01-01", | ||
| "2020-01-02", | ||
| "2020-01-03", | ||
| "2020-02-01", | ||
| "2020-02-02", | ||
| "2020-02-03", | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| other_value = { | ||
| "target": "MIDS", | ||
| "regex": ".*?(\\d+)$", # Non-greedy to correctly extract multi-digit numbers | ||
| "within": ["USUBJID", "MIDSTYPE"], | ||
| "comparator": [ | ||
| {"name": "SMSTDTC", "sort_order": "ASC", "null_position": "first"} | ||
| ], | ||
| } | ||
|
|
||
| result = DataframeType({"value": df}).target_is_sorted_by(other_value) | ||
| assert result.equals(pd.Series([True, True, True, True, True, True])) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("dataset_class", [PandasDataset, DaskDataset]) | ||
| def test_target_is_sorted_by_without_regex_still_works(dataset_class): | ||
| """ | ||
| Test that the operator still works without regex (backward compatibility). | ||
| """ | ||
| df = dataset_class.from_dict( | ||
| { | ||
| "USUBJID": ["001", "001", "001"], | ||
| "SESEQ": [1, 2, 3], | ||
| "SESTDTC": [ | ||
| "2020-01-01", | ||
| "2020-01-02", | ||
| "2020-01-03", | ||
| ], | ||
| } | ||
| ) | ||
|
|
||
| other_value = { | ||
| "target": "SESEQ", | ||
| "within": "USUBJID", | ||
| "comparator": [{"name": "SESTDTC", "sort_order": "ASC"}], | ||
| } | ||
|
|
||
| result = DataframeType({"value": df}).target_is_sorted_by(other_value) | ||
| assert result.equals(pd.Series([True, True, True])) |
Oops, something went wrong.
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.
I think we should add the new regex property here.
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.
It’s not very visible here, but I actually added information about the regex. It’s easier to view it in the editor.