-
Notifications
You must be signed in to change notification settings - Fork 42
587 Core Rules 1689 Blocked Fix #1814
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
Open
aniemes
wants to merge
22
commits into
main
Choose a base branch
from
587_corerules_1689
base: main
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.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7891ef4
Add regex_find_replace operation and CoreIssue587 coverage
a254ce5
Merge branch 'main' into 587_corerules_1689
aniemes edaea85
Update merged rule schema files
99ef3b3
Merge branch '587_corerules_1689' of https://github.com/aniemes/cdisc…
c81894d
Adjusted for PR feedback
f83f46d
Merge branch 'main' into 587_corerules_1689
RamilCDISC b2042a0
Format: apply Black formatting to regex_find_replace.py
9498d65
Merge branch '587_corerules_1689' of https://github.com/aniemes/cdisc…
78bec90
Merge branch 'main' into 587_corerules_1689
aniemes 86621ea
Merge branch 'main' into 587_corerules_1689
aniemes 44536bd
Addressed PR feedback
5912f98
Merge branch 'main' into 587_corerules_1689
68def4d
Merge branch '587_corerules_1689' of https://github.com/cdisc-org/cdi…
1009337
Merge branch 'main' into 587_corerules_1689
aniemes 32bf47e
Adjusted to fix script errors
312c92d
Merge branch '587_corerules_1689' of https://github.com/cdisc-org/cdi…
b4752db
Fix W292: add missing newline at end of file
126caf4
Added newline
28fbaf1
Fix W292: re-add missing newline at end of file
94e8c2b
Update merged schema files with markdown descriptions
f3ec928
Fix W391: remove extra blank line at end of file
e63ac1f
Apply black formatting to regex_find_replace
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import re | ||
|
|
||
| NO_MATCH_POLICIES = {"keep_original", "set_null", "set_empty", "error"} | ||
|
|
||
| REGEX_FLAG_MAP = { | ||
| "i": re.IGNORECASE, | ||
| "m": re.MULTILINE, | ||
| "s": re.DOTALL, | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import re | ||
| import pandas as pd | ||
|
|
||
| from cdisc_rules_engine.operations.base_operation import BaseOperation | ||
| from cdisc_rules_engine.exceptions.custom_exceptions import OperationError | ||
| from cdisc_rules_engine.constants.operation_constants import ( | ||
| NO_MATCH_POLICIES, | ||
| REGEX_FLAG_MAP, | ||
| ) | ||
|
|
||
|
|
||
| class RegexFindReplace(BaseOperation): | ||
| def _execute_operation(self): | ||
| """ | ||
| Finds and replaces text in a target column using regex pattern matching. | ||
| Returns a Series with transformed values based on the replace pattern and no_match policy. | ||
| """ | ||
| operation_id = self.params.operation_id | ||
| target = self.params.target | ||
| find = getattr(self.params, "find", None) or getattr(self.params, "regex", None) | ||
| replace = getattr(self.params, "replace", None) | ||
| on_no_match = getattr(self.params, "on_no_match", "keep_original") | ||
| flags_str = getattr(self.params, "flags", "") | ||
|
|
||
| self._validate_required( | ||
| operation_id, target, find, replace, on_no_match, flags_str | ||
| ) | ||
|
|
||
| if target not in self.evaluation_dataset.columns: | ||
| raise OperationError(f"Target column not found: {target}") | ||
|
|
||
| flags = self._parse_flags(flags_str) | ||
| pattern = self._compile_pattern(find, flags) | ||
|
|
||
| source = self.evaluation_dataset[target] | ||
| transformed = source.map( | ||
| lambda value: self._transform_value( | ||
| value=value, | ||
| pattern=pattern, | ||
| replace=replace, | ||
| on_no_match=on_no_match, | ||
| ) | ||
| ) | ||
|
|
||
| return transformed | ||
|
|
||
| def _validate_required( | ||
| self, operation_id, target, find, replace, on_no_match, flags_str | ||
| ): | ||
| if not operation_id: | ||
| raise OperationError("regex_find_replace requires id (operation_id)") | ||
| if not target: | ||
| raise OperationError("regex_find_replace requires name (target)") | ||
| if not find: | ||
| raise OperationError("regex_find_replace requires find (or regex)") | ||
| if replace is None: | ||
| raise OperationError("regex_find_replace requires replace") | ||
| if on_no_match not in NO_MATCH_POLICIES: | ||
| raise OperationError( | ||
| f"Invalid on_no_match: {on_no_match}. " | ||
| f"Must be one of {sorted(NO_MATCH_POLICIES)}" | ||
| ) | ||
| invalid_flags = [f for f in flags_str if f not in REGEX_FLAG_MAP] | ||
| if invalid_flags: | ||
| raise OperationError( | ||
| f"Invalid flags: {''.join(invalid_flags)}. " | ||
| f"Allowed flags: {''.join(sorted(REGEX_FLAG_MAP.keys()))}" | ||
| ) | ||
|
|
||
| def _parse_flags(self, flags_str): | ||
| flags = 0 | ||
| for ch in flags_str: | ||
| flags |= REGEX_FLAG_MAP[ch] | ||
| return flags | ||
|
|
||
| def _compile_pattern(self, find, flags): | ||
| try: | ||
| return re.compile(find, flags) | ||
| except re.error as exc: | ||
| raise OperationError(f"Invalid regex pattern '{find}': {exc}") from exc | ||
|
|
||
| def _transform_value(self, value, pattern, replace, on_no_match): | ||
| if value is None or (isinstance(value, float) and pd.isna(value)): | ||
| return None | ||
|
|
||
| text = str(value) | ||
| match = pattern.search(text) | ||
| if match: | ||
| try: | ||
| return pattern.sub(replace, text) | ||
| except re.error as exc: | ||
| raise OperationError( | ||
| f"Error applying regex pattern '{pattern.pattern}' to value '{text}': {exc}" | ||
| ) from exc | ||
|
|
||
| if on_no_match == "keep_original": | ||
| return text | ||
| if on_no_match == "set_null": | ||
| return None | ||
| if on_no_match == "set_empty": | ||
| return "" | ||
| raise OperationError( | ||
| f"No match found for value '{text}' and on_no_match='error'" | ||
| ) | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.